comp.lang.ada
 help / color / mirror / Atom feed
* Re: Pitfall: freeing access discriminants
@ 2003-02-13 12:41 Grein, Christoph
  2003-02-13 13:38 ` Victor Porton
  0 siblings, 1 reply; 15+ messages in thread
From: Grein, Christoph @ 2003-02-13 12:41 UTC (permalink / raw)
  To: comp.lang.ada

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;



^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: Pitfall: freeing access discriminants
@ 2003-02-14  6:21 Grein, Christoph
  0 siblings, 0 replies; 15+ messages in thread
From: Grein, Christoph @ 2003-02-14  6:21 UTC (permalink / raw)
  To: comp.lang.ada

> >   type Integer_Ptr is access all Integer;
> >...
> >   P: Integer_Ptr := Integer_Ptr (Object.D);
> According to Cohen 2nd ed p 361, Integer_Ptr cannot be a pool-specific
> access type.  And how would you make the anonymous access type
> "D: access Integer" allocate from other than the standard pool?

There are some statements in the AARM that you have to deallocate with an access 
ess type that has the same storage pool as the one the object was allocated 
with. Without specifying that two access types accessing the same type actually 
use the same pool (via specifying the pool of the second access type: "for 
P2'Storage_Pool use P1'Storage_Pools;" - not sure of the syntax), you rely on 
the mercy of the compiler implementers.

You might experience the nasal demons 
effect...



^ permalink raw reply	[flat|nested] 15+ messages in thread
* Re: Pitfall: freeing access discriminants
@ 2003-02-14  6:02 Grein, Christoph
  0 siblings, 0 replies; 15+ messages in thread
From: Grein, Christoph @ 2003-02-14  6:02 UTC (permalink / raw)
  To: comp.lang.ada

>   In this case, you can shoot safely away from your foot with
> Christoph Grein's suggested:
> > 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;
> 
> I seems to me that you needn't worry that
> > Because of storage pools for Integer_Ptr may be different
> > from storage pool used by "new".
> because you get a standard storage pool unless you specify otherwise, so
> > type T(D: access Integer)
> >   type Integer_Ptr is access all Integer;
> should both refer to the same storage pool.

Hm, I wouldn't be so sure... Better to consult the implementor's manuals. There is nothing in the RM that prevents an implementation from selecting different 
"standard" storage pools for objects allocated for access types and those 
allocated for access discriminants.

Gnat for instance uses different pools depending on the kind of allocation, so 
cannot be sure that access types without a named pool use the same pool.



^ permalink raw reply	[flat|nested] 15+ messages in thread
* Pitfall: freeing access discriminants
@ 2003-02-13 10:04 Victor Porton
  2003-02-13 12:07 ` Victor Porton
  2003-02-13 17:40 ` Stephen Leake
  0 siblings, 2 replies; 15+ messages in thread
From: Victor Porton @ 2003-02-13 10:04 UTC (permalink / raw)


type Integer_Access is access Integer;

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

procedure Finalize(Object: in out T) is
  -- Error: cannot convert access discriminant to non-local access type
  D: Integer_Access := Integer_Access(Object.D);
begin
  Free(D);
end;

What to do? It seems being a serious deficiency in Ada or there are a 
workaround?



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

end of thread, other threads:[~2003-02-22 19:00 UTC | newest]

Thread overview: 15+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2003-02-13 12:41 Pitfall: freeing access discriminants Grein, Christoph
2003-02-13 13:38 ` 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

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