comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Unchecked_Deallocation of class-wide objects
Date: Mon, 28 Sep 2009 11:12:04 +0200
Date: 2009-09-28T11:12:05+02:00	[thread overview]
Message-ID: <ztz0jw6q453f.11t7i5yf6ibrd$.dlg@40tude.net> (raw)
In-Reply-To: 9b4410c1-17a9-434c-b0e5-75843364ca36@r31g2000vbi.googlegroups.com

On Mon, 28 Sep 2009 01:43:03 -0700 (PDT), Maciej Sobczak wrote:

> Is it legal and safe to deallocate class-wide objects?
> 
> The problem is that such an object is allocated with its concrete
> type, whereas deallocation is defined for its class-wide type.

Yes, deallocation "dispatches" on the pointer's target.

> Consider:
> 
> type Shape is tagged private;
> type Shape_Access is access Shape'Class;
> 
> procedure Free_Shape is new Ada.Unchecked_Deallocation
>    (Object => Shape'Class, Name => Shape_Access);
> -- ...
> 
> type Circle is new Shape with ...
> -- ...
> C : Shape_Access := new Circle;
> -- ...
> Free_Shape (C);
> 
> Is the Circle object allocated on the Shape-wide storage pool? From
> what I understand, this is the condition for the above to work
> properly.

In Ada pool is bound to the access type, not to the target type, which is
logical consequence that an object can be allocated on the stack.

Another consequence is that it is meaningless to talk about
Shape-wide-pool, however an implementation may indeed allocate objects of
different types in different pools transparently to the program. If it
chooses to do this for tagged types of the same hierarchy, then the pointer
should become fat and contain the type tag in it. I know no Ada compiler
that does it this way, but it is a possible scheme, IMO.

> What if Circle is allocated for some Circle_Access type which is then
> converted to Shape_Access? Can it be safely deallocated?

You could not convert it because Shape_Access is pool-specific.
(Unchecked_Conversion tells for itself)

If it were a general access to class wide then deallocator would be "doubly
dispatching" on the pool and on the target. Thus, as far as I can tell, it
is safe in both cases.

-- 
Regards,
Dmitry A. Kazakov
http://www.dmitry-kazakov.de



      reply	other threads:[~2009-09-28  9:12 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2009-09-28  8:43 Unchecked_Deallocation of class-wide objects Maciej Sobczak
2009-09-28  9:12 ` Dmitry A. Kazakov [this message]
replies disabled

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