comp.lang.ada
 help / color / mirror / Atom feed
From: "Dmitry A. Kazakov" <mailbox@dmitry-kazakov.de>
Subject: Re: Deallocation & Tagged Types
Date: Sun, 19 Oct 2003 18:10:29 +0200
Date: 2003-10-19T18:10:29+02:00	[thread overview]
Message-ID: <bmucnk$r3f6v$1@ID-77047.news.uni-berlin.de> (raw)
In-Reply-To: 5Fhkb.3195$KA5.27417@newsfep4-glfd.server.ntli.net

chris wrote:

> I've got a situation involving deallocating an object from a proxy and
> am a bit stuck...  Namely how to deallocate an object belonging to a
> particular class but not knowing the specific type.
> The example is not from the code, it just highlights the problem...
> 
> -- a vehicles package
> --
> package Vehicles is
> 
>     type Vehicle is abstract tagged null record;
>     type Vehicle_Access is access all Vehicle'class;
> 
>     procedure Destroy (V : in out Vehicle) is abstract;
> 
> end Vehicles;
> 
> -- proxies for vehicles
> --
> package Vehicle_Proxy is
> 
>     type Proxy is new Vehicle with private;
> 
>     procedure Destroy (V : in out Proxy);
> 
> private
>     type Proxy is new Vehicle with record
>        Actual : Vehicle_Access;
>     end record;
> 
> end Vehicle_Proxy;
> 
> package body Vehicle_Proxy is
> 
>     procedure Destroy (V : in out Proxy) is
>     begin
>        Destroy (V.Actual.all);
> 
>        -- now what???

Same as with any other access type (except an access to subprogram, mind you
(:-)):

function Free is
   new Ada.Unchecked_Deallocation (Vehicle'Class, Vehicle_Access);

Free (V.Actual);
   -- Kills V.Actual and sets V.Actual to null,
   -- or does nothing if V.Actual is already null

Note that Free releases a _class-wide_ object. So it first dispatches to
whatever language-defined finalization is required (for a controlled type
it is Finalize) then it deallocates the memory.

BTW, it is better to make Vehicle and Proxy controlled and name Destroy
"Finalize", which will be then called implicitly, and note always the right
one. It need not to be abstract because the type Vehicle is already
abstract, so no harm can be done anyway.

>     end Destroy;
> 
> end Vehicle_Proxy;
> 
> If V.Actual isn't properly disposed of, the proxy will leak.  What's the
> best way to handle this?  One way I can think of is to do something like
> this...
> [...]

This is what tagged types are for. And note that Ada is a very consistent
and consequent language. If it allows class-wide objects, it also allows
them destroyed in a reasonable way.

> Is there another way that I'm missing or is this how people do this in
> their code?

For example:

http://www.dmitry-kazakov.de/ada/components.htm

> I also came up against this when making a container type (a
> container for rasters in the real code).  Namely if you stuff
> Raster_Access into a container (an Image) and destroy the Image
> container, you need to deallocate the rasters.  That means you need to
> have a deallocator capable of handling the types derived from Raster.
> If you miss one, the code breaks.

See above.

-- 
Regards,
Dmitry A. Kazakov
www.dmitry-kazakov.de



  parent reply	other threads:[~2003-10-19 16:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2003-10-18 20:53 Deallocation & Tagged Types chris
2003-10-19 15:38 ` Robert I. Eachus
2003-10-19 16:53   ` chris
2003-10-20  1:08     ` Robert I. Eachus
2003-10-19 16:10 ` Dmitry A. Kazakov [this message]
2003-10-19 16:50   ` chris
2003-10-20  1:13     ` Robert I. Eachus
2003-10-20  1:43       ` Hyman Rosen
2003-10-26  8:11 ` Patrice Freydiere
replies disabled

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