From: Maciej Sobczak <see.my.homepage@gmail.com>
Subject: Limited_Controlled and out parameters
Date: Fri, 22 Jun 2012 06:37:31 -0700 (PDT)
Date: 2012-06-22T06:37:31-07:00 [thread overview]
Message-ID: <544352ec-9d9e-4f08-8629-50ef394ab846@googlegroups.com> (raw)
I have found a conceptual problem with the rules for Limited_Controlled types when they are used as out parameters in subprograms.
Consider:
type Int_Ptr is access Integer;
type Int_Holder is new Limited_Controlled with record
I : Int_Ptr;
end record;
The intent is that the Int_Holder can optionally hold an integer value. Optionally means that it might as well be empty (null);
Then:
procedure Set (H : out Holder; Value : in Integer) is
begin
H.I := new Integer'(Value);
end Set;
All is fine and the conveniently overriding procedure Finalize cleans the mess at the end.
The problem is that the Set procedure can be called many times in a row:
Set (H, 7);
Set (H, 8);
-- ...
Each time it is called on the same H (Holder) object, a new Integer is allocated - this is a memory leak.
A possible solution is to check if the holder already contains some value and deallocate it before planting a new one. The problem is - this is an *out* parameter and notionally it is uninitialized. Well, controlled types are passed by reference, so it *would* work fine, but that's just cheating (and a more strict data flow would reject it as a clear violation).
What's the proper solution?
--
Maciej Sobczak * http://www.msobczak.com * http://www.inspirel.com
next reply other threads:[~2012-06-22 13:39 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2012-06-22 13:37 Maciej Sobczak [this message]
2012-06-22 15:43 ` Limited_Controlled and out parameters Adam Beneschan
2012-06-22 16:58 ` J-P. Rosen
2012-06-22 19:12 ` Adam Beneschan
2012-06-22 21:41 ` Robert A Duff
2012-06-22 20:22 ` Randy Brukardt
2012-06-23 12:02 ` Maciej Sobczak
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox