[fpc-pascal] Double finalize

Hairy Pixels genericptr at gmail.com
Thu Dec 21 06:06:17 CET 2023



> On Dec 21, 2023, at 6:11 AM, Hairy Pixels <genericptr at gmail.com> wrote:
> 
> 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.

Here's another example of this. If I use an array Finalize is called 4 times but  Initialize is never called, unless you assign  a record to the array and then it's called once.

This makes even less sense. What's the idea behind this behavior? I would expect the calls to balanced as this feature is intended for reference counting I thought.


==============================

{$mode objfpc}
{$modeswitch advancedrecords}

program test;

type
 TManagedObject = record
   x: integer;
   class operator Finalize(var self: TManagedObject); inline;
   class operator Initialize(var self: TManagedObject); inline;
 end;

class operator TManagedObject.Finalize(var self: TManagedObject);
begin
 writeln('Finalize');
end;

class operator TManagedObject.Initialize(var self: TManagedObject);
begin
 writeln('Initialize');
end;

var
 a: array[0..3] of TManagedObject;
begin
 a[0].x := 1;
 a[1].x := 1;
 a[2].x := 1;
 a[3].x := 1;
end.

Regards,
Ryan Joseph



More information about the fpc-pascal mailing list