comp.lang.ada
 help / color / mirror / Atom feed
From: "Grein, Christoph" <christoph.grein@eurocopter.com>
To: comp.lang.ada@ada.eu.org
Subject: Re: Pitfall: freeing access discriminants
Date: Thu, 13 Feb 2003 13:41:00 +0100 (MET)
Date: 2003-02-13T13:41:00+01:00	[thread overview]
Message-ID: <mailman.4.1045140569.13246.comp.lang.ada@ada.eu.org> (raw)

This works, but you have to be careful with the allocation pools.
Also access discriminants can never be null, so if you deallocate the object it 
is accessing, ...

Ok, here you are safe since you finalize the whole object.

with Ada.Finalization;

package Acc_Dis is

  type T (D: access Integer) is
    new Ada.Finalization.Limited_Controlled with null record;

  procedure Finalize (Object: in out T);

  V: T (new Integer'(5));

end Acc_Dis;
with Ada.Unchecked_Deallocation;

package body Acc_Dis is

  procedure Finalize (Object: in out T) is
    type Integer_Ptr is access all Integer;
    procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_Ptr);
    P: Integer_Ptr := Integer_Ptr (Object.D);
  begin
    Free (P);
  end Finalize;

end Acc_Dis;
-----------------------
Also this works:

with Ada.Finalization;

package Acc_Dis is

  type Integer_Ptr is access Integer;

  type T (D: Integer_Ptr) is
    new Ada.Finalization.[Limited_]Controlled with null record;
  -- An access subtype discriminate does not afford a limited type.

  procedure Finalize (Object: in out T);

  V: T (new Integer'(5));

end Acc_Dis;
with Ada.Unchecked_Deallocation;

package body Acc_Dis is

  procedure Free is new Ada.Unchecked_Deallocation (Integer, Integer_Ptr);

  procedure Finalize (Object: in out T) is
    P: Integer_Ptr := Object.D;
  begin
    Free (P);
  end Finalize;

end Acc_Dis;



             reply	other threads:[~2003-02-13 12:41 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-02-13 12:41 Grein, Christoph [this message]
2003-02-13 13:38 ` Pitfall: freeing access discriminants Victor Porton
2003-02-13 20:49   ` tmoran
2003-02-13 21:17     ` Simon Wright
2003-02-14  4:16       ` tmoran
2003-02-14 21:30         ` Simon Wright
2003-02-14  8:28     ` Victor Porton
2003-02-22 19:00       ` Robert A Duff
  -- strict thread matches above, loose matches on Subject: below --
2003-02-14  6:21 Grein, Christoph
2003-02-14  6:02 Grein, Christoph
2003-02-13 10:04 Victor Porton
2003-02-13 12:07 ` Victor Porton
2003-02-13 13:42   ` Preben Randhol
2003-02-13 17:40 ` Stephen Leake
2003-02-13 19:24   ` Victor Porton
replies disabled

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