comp.lang.ada
 help / color / mirror / Atom feed
* freeing general access types
@ 2002-12-23 23:19 tmoran
  2002-12-23 23:35 ` Robert A Duff
  2002-12-24 14:36 ` Stephen Leake
  0 siblings, 2 replies; 3+ messages in thread
From: tmoran @ 2002-12-23 23:19 UTC (permalink / raw)


If P is a general access type, is there any way to know whether it's
pointing to allocated storage, and should have Ada.Unchecked_Deallocation
applied to avoid a memory leak, or it's pointing at a declared
object, which should not be deallocated?



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

* Re: freeing general access types
  2002-12-23 23:19 freeing general access types tmoran
@ 2002-12-23 23:35 ` Robert A Duff
  2002-12-24 14:36 ` Stephen Leake
  1 sibling, 0 replies; 3+ messages in thread
From: Robert A Duff @ 2002-12-23 23:35 UTC (permalink / raw)


tmoran@acm.org writes:

> If P is a general access type, is there any way to know whether it's
> pointing to allocated storage, and should have Ada.Unchecked_Deallocation
> applied to avoid a memory leak, or it's pointing at a declared
> object, which should not be deallocated?

No, other than whatever logic is in your program to keep track.

Note that you also need to make sure you are deallocating from the right
pool.  If you have a value of a general access type, you do not
generally know that it was allocated from the pool of that type.
You must convert it to the right type before deallocating.

I have written pool types where you can tell at run time which pool (if
any) a given pointer is pointing into.  But the more common thing is to
encapsulate allocation and deallocation in a small place where you can
understand (at compile time) what's going on.

Many access types are not used for allocation/deallocation at all.
They are used only for 'Access and/or conversion from other types.
It's a good idea to write:

    for Acc_Type'Storage_Size use 0;

for such types; then you will usually get a warning if you try to do a
"new".  And a run-time error.

- Bob



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

* Re: freeing general access types
  2002-12-23 23:19 freeing general access types tmoran
  2002-12-23 23:35 ` Robert A Duff
@ 2002-12-24 14:36 ` Stephen Leake
  1 sibling, 0 replies; 3+ messages in thread
From: Stephen Leake @ 2002-12-24 14:36 UTC (permalink / raw)


tmoran@acm.org writes:

> If P is a general access type, is there any way to know whether it's
> pointing to allocated storage, and should have Ada.Unchecked_Deallocation
> applied to avoid a memory leak, or it's pointing at a declared
> object, which should not be deallocated?

I don't think so. I use naming conventions and distinct types for
that; one access type either _always_ points to constants, or _always_
should be deallocated.

Or you can store info inside the type that gives this information.

-- 
-- Stephe



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

end of thread, other threads:[~2002-12-24 14:36 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-12-23 23:19 freeing general access types tmoran
2002-12-23 23:35 ` Robert A Duff
2002-12-24 14:36 ` Stephen Leake

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