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=-1.9 required=5.0 tests=BAYES_00 autolearn=ham autolearn_force=no version=3.4.4 X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: 103376,4f0b59438ce4c5b9,start X-Google-Attributes: gid103376,public X-Google-ArrivalTime: 2003-10-18 13:46:57 PST Path: archiver1.google.com!news2.google.com!news.maxwell.syr.edu!newsfeed.stueberl.de!newspeer1-gui.server.ntli.net!ntli.net!newsfep4-glfd.server.ntli.net.POSTED!53ab2750!not-for-mail From: chris User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.5) Gecko/20031014 Thunderbird/0.3 X-Accept-Language: en-us, en MIME-Version: 1.0 Newsgroups: comp.lang.ada Subject: Deallocation & Tagged Types Content-Type: text/plain; charset=us-ascii; format=flowed Content-Transfer-Encoding: 7bit Message-ID: <5Fhkb.3195$KA5.27417@newsfep4-glfd.server.ntli.net> Date: Sat, 18 Oct 2003 21:53:02 +0100 NNTP-Posting-Host: 81.98.236.129 X-Complaints-To: abuse@ntlworld.com X-Trace: newsfep4-glfd.server.ntli.net 1066510017 81.98.236.129 (Sat, 18 Oct 2003 21:46:57 BST) NNTP-Posting-Date: Sat, 18 Oct 2003 21:46:57 BST Organization: ntl Cablemodem News Service Xref: archiver1.google.com comp.lang.ada:1122 Date: 2003-10-18T21:53:02+01:00 List-Id: Hi, 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??? 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... type Vehicle_Deallocator is access procedure (V : in out Vehicle_Access); and revise type Proxy to the following, type Proxy is new Vehicle with record Actual : Vehicle_Access; Dealloc : Vehicle_Deallocator; end record; and Destroy to procedure Destroy (V : in out Proxy) is begin Destroy (V.Actual.all); V.Dealloc (V.Actual); V.Actual := null; end Destroy; Is there another way that I'm missing or is this how people do this in their code? 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. The alternative is to deallocate each raster outside of the image container, but you still need someway of handling each subclass of raster. Cheers, Chris