comp.lang.ada
 help / color / mirror / Atom feed
* Limited_Controlled and out parameters
@ 2012-06-22 13:37 Maciej Sobczak
  2012-06-22 15:43 ` Adam Beneschan
  0 siblings, 1 reply; 7+ messages in thread
From: Maciej Sobczak @ 2012-06-22 13:37 UTC (permalink / 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



^ permalink raw reply	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2012-06-23 12:02 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-06-22 13:37 Limited_Controlled and out parameters Maciej Sobczak
2012-06-22 15:43 ` 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox