[fpc-pascal] Arguments gets corrupted with anonymous nested function

Hairy Pixels genericptr at gmail.com
Sun Sep 4 18:07:26 CEST 2022


The following program uses an anonymous nested function which captures a local variable. It seems the arguments get corrupted (likely because of the hidden first param for the nested function signature) and prints garbled data. I reduced this snippet down from another project but I’m not sure if this is the best way to reproduce the real issue.

Is this a compiler bug or am I doing something wrong?

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

{$mode objfpc}
{$modeswitch anonymousfunctions}
{$modeswitch nestedprocvars}

program test;

type
  generic TMyClass<T> = class
    type
      TCallback = procedure (data: T) is nested;

    procedure Call(callback: TCallback);
    procedure Test;
  end;

procedure TMyClass.Call(callback: TCallback);
begin
  callback(default(T));
end;

procedure TMyClass.Test;
var
  i: integer;
begin
  Call(procedure(data: T)
  begin
    Inc(i);
    writeln(data);  // prints garbled data like 73251232
  end);
end;

begin
  specialize TMyClass<Integer>.Create.Test;
end.


Regards,
	Ryan Joseph



More information about the fpc-pascal mailing list