From: Blady <p.p11@orange.fr>
Subject: Re: Unchecked_Deallocation with tagged class type.
Date: Wed, 15 Nov 2023 21:26:06 +0100 [thread overview]
Message-ID: <uj39gv$1srlq$1@dont-email.me> (raw)
In-Reply-To: <uj0t5j$1dp63$1@dont-email.me>
Le 14/11/2023 à 23:42, Dmitry A. Kazakov a écrit :
>> 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.
Thanks, I haven't considered this possibility.
Note: the previous program built with GNAT FSF 13.2.0 ran without exception.
I've changed:
with Ada.Unchecked_Deallocation;
procedure test_20231113_free_class 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'Class, CATTA);
begin
Free (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_class;
It runs without exception.
One question remains about "Finalize (OB);":
Which memory size is deallocated TTA'Size or TTB'Size?
Thanks, Pascal.
next prev parent reply other threads:[~2023-11-15 20:26 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-14 21:11 Unchecked_Deallocation with tagged class type Blady
2023-11-14 22:42 ` Dmitry A. Kazakov
2023-11-15 20:26 ` Blady [this message]
2023-11-15 21:17 ` Dmitry A. Kazakov
2023-11-16 20:29 ` Blady
2023-11-17 13:12 ` AdaMagica
2023-11-17 13:13 ` AdaMagica
replies disabled
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox