From mboxrd@z Thu Jan 1 00:00:00 1970 Path: eternal-september.org!news.eternal-september.org!.POSTED!not-for-mail From: "Dmitry A. Kazakov" Newsgroups: comp.lang.ada Subject: Re: Unchecked_Deallocation with tagged class type. Date: Tue, 14 Nov 2023 23:42:59 +0100 Organization: A noiseless patient Spider Message-ID: References: MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit Injection-Date: Tue, 14 Nov 2023 22:42:59 -0000 (UTC) Injection-Info: dont-email.me; posting-host="194c7c3c86974836b515613134ba380a"; logging-data="1500355"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+soVppmemtbY17W6uSu+mcxt4yFtjS6A8=" User-Agent: Mozilla Thunderbird Cancel-Lock: sha1:LSfFRDZ7oL5LZimezTmPN5GOt+k= In-Reply-To: Content-Language: en-US Xref: news.eternal-september.org comp.lang.ada:65846 List-Id: On 2023-11-14 22:11, Blady wrote: > The following code present a Finalize procedure with a parameter of > access tagged class type in order to deallocate the memory of the given > parameter from the root tagged type TTA and his children. > The same for TTB which is inherited from TTA. But this Finalize call > Finalize of TTA. > It may be not the best idea. > But let's see: > > with Ada.Unchecked_Deallocation; > procedure test_20231113_free_tag is > >    type TTA is tagged record >       AA : Integer; >    end record; >    type ATTA is access all TTA; >    type CATTA is access all TTA'Class; >    procedure Finalize (O : in out CATTA) is >       procedure Free is new Ada.Unchecked_Deallocation (TTA, ATTA); >    begin >       Free (ATTA (O)); >    end Finalize; > >    type TTB is new TTA with record >       BB : Integer; >    end record; >    type ATTB is access all TTB; >    type CATTB is access all TTB'Class; >    procedure Finalize (O : in out CATTB) is >    begin >       Finalize (CATTA (O)); >    end Finalize; > >    OA : CATTA := new TTA; >    OB : CATTB := new TTB; > > begin >    Finalize (OA); >    Finalize (OB); > end test_20231113_free_tag; > > The procedure Free is the instanciation of Unchecked_Deallocation with > the tagged type TTA. > Thus the call "Finalize (OA);" deallocate the memory of object OA of > type access class TTA. > > But what does "Finalize (OB);"? Crashes your program. It is a bug. You should instantiate Unchecked_Deallocation with class-wide type if you pass a class-wide pointer. -- Regards, Dmitry A. Kazakov http://www.dmitry-kazakov.de