Skip to content

Function references: assignment corruption at the second assignment

Summary

I have 2 different methods and 2 different function reference variables. I assign each of them (one per one), both variables receive the same method.

System Information

  • Operating system: Windows
  • Processor architecture: x86
  • Compiler version: trunk
  • Device: Computer

Steps to reproduce

Run the example project.

Example Project

program TestRefProcAssignment;

{$mode objfpc}{$H+}
{$ModeSwitch functionreferences}

type
  TNotifyProc = reference to procedure();

  TMyClass = class
  public
    procedure One;
    procedure Two;
  end;

{ TMyClass }

var
  OneCounter: Integer = 0;
  TwoCounter: Integer = 0;

procedure TMyClass.One;
begin
  Writeln('One');
  Inc(OneCounter);
end;

procedure TMyClass.Two;
begin
  Writeln('Two');
  Inc(TwoCounter);
end;

var
  One, Two: TNotifyProc;
  MyObject: TMyClass;
  HasError: Boolean = False;
begin
  MyObject := TMyClass.Create;
  One := @MyObject.One;
  Two := @MyObject.Two;
  One(); // writes out One - OK
  Two(); // writes out One - Error

  if One=Two then // yes, they are equal - Error
    HasError := True;
  if not ((OneCounter=1) and (TwoCounter=1)) then // Error: OneCounter=2, TwoCounter=0
    HasError := True;

  if HasError then
    Halt(1);
end.

What is the current bug behavior?

The variables One and Two get both the @One method pointer;

What is the expected (correct) behavior?

One should get @One, Two should get @Two.

Edited by Sven/Sarah Barth
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information