[fpc-pascal] Double finalize

Nikolay Nikolov nickysn at gmail.com
Thu Dec 21 06:29:59 CET 2023


On 12/21/23 01:11, Hairy Pixels via fpc-pascal wrote:
>
>> On Dec 21, 2023, at 1:53 AM, Michael Van Canneyt via fpc-pascal <fpc-pascal at lists.freepascal.org> wrote:
>>
>> If you look at the generated code, you see that there is an implicit try/finally block
>> and the  finally block does a finalize.
> Maybe I misunderstood but I thought they were supposed to be balanced with init calls. Is it the design of the compiler to allow multiple finalize calls and have the user keep track of it the underlying structure is really finalized or not? If you were doing things like deleting memory in the finalizer you would have a double free happen here.

Note that you should also add an 'AddRef' and 'Copy' management 
operators and implement these as well. However, I think it's an FPC bug 
that Initialize is called twice in this case. I could be wrong, though.

If you assign the result to a local variable, i.e.:

var

   a: TManagedObject;

begin

   a := TManagedObject.Create;

end.

You get the following output:

Initialize
Initialize
Create
Copy
Finalize
Finalize

So, I think there's a bug in FPC that finalize is called twice when the 
return value is discarded.

Nikolay



More information about the fpc-pascal mailing list