From mboxrd@z Thu Jan 1 00:00:00 1970 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on polar.synack.me X-Spam-Level: X-Spam-Status: No, score=-0.3 required=5.0 tests=BAYES_00, REPLYTO_WITHOUT_TO_CC autolearn=no autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4f0b59438ce4c5b9 X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-19 09:05:11 PST Path: archiver1.google.com!news2.google.com!fu-berlin.de!uni-berlin.de!dialin-145-254-036-203.arcor-ip.NET!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Deallocation & Tagged Types Date: Sun, 19 Oct 2003 18:10:29 +0200 Organization: At home Message-ID: References: <5Fhkb.3195$KA5.27417@newsfep4-glfd.server.ntli.net> Reply-To: mailbox@dmitry-kazakov.de NNTP-Posting-Host: dialin-145-254-036-203.arcor-ip.net (145.254.36.203) Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de 1066579509 28425439 145.254.36.203 (16 [77047]) User-Agent: KNode/0.7.2 Xref: archiver1.google.com comp.lang.ada:1138 Date: 2003-10-19T18:10:29+02:00 List-Id: 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