Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2023-03

Function reference confuses function pointer?

6 messages · 3 people ·

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Hairy Pixels

This caused me a nasty debugging confusion, it’s a bug right? In the program below the two parameters passed to TBinding.Create have the same address despite being function pointers to two different class methods. Calling them both call the same method which is clearly not correct.

Free Pascal Compiler version 3.3.1 [2022/09/28] for aarch64

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

{$mode objfpc}
{$modeswitch functionreferences}
{$modeswitch advancedrecords}
program test;
type
  generic TBinding<T> = record
    private type
      TGetter = reference to function: T;
      TSetter = reference to procedure(newValue: T);
    private
      getter: TGetter;
      setter: TSetter;
    public
      constructor Create(_getter: TGetter; _setter: TSetter);
  end;
constructor TBinding.Create(_getter: TGetter; _setter: TSetter);
begin
  getter := _getter;
  setter := _setter;
  // Same address for both!
  writeln(HexStr(getter));
  writeln(HexStr(setter));
end;
type
  TMyClass = class
    procedure SetSelection(newValue: Integer);
    function GetSelection: Integer;
    procedure AfterConstruction; override;
  end;
function TMyClass.GetSelection: Integer;
begin
end;
procedure TMyClass.SetSelection(newValue: Integer);
begin
end;
procedure TMyClass.AfterConstruction;
begin
  specialize TBinding<Integer>.Create(@GetSelection, @SetSelection);
end;
begin
  TMyClass.Create;
end.

Regards,
Ryan Joseph

Dwight Schauer
to Hairy Pixels
Hairy Pixels wrote
This caused me a nasty debugging confusion, it’s a bug right? In the program below the two parameters passed to TBinding.Create have the same address despite being function pointers to two different class methods. Calling them both call the same method which is clearly not correct. Free Pascal Compiler version 3.3.1 [2022/09/28] for aarch64

2022/09/28?

Does not do this 3.3.1 [2023/03/02] for me. I get different addresses.

$ fpc same.pas
Free Pascal Compiler version 3.3.1 [2023/03/02] for x86_64 Copyright (c) 1993-2023 by Florian Klaempfl and others Target OS: Linux for x86-64
Compiling same.pas
same.pas(37,19) Warning: Function result does not seem to be set Linking same
52 lines compiled, 0.2 sec, 144256 bytes code, 57848 bytes data 1 warning(s) issued

$ ./same
00007FA3B433A080
00007FA3B433A078

Hairy Pixels
to Dwight Schauer

Ah maybe this is the target/platform. I’m on macOS:

Target OS: Darwin for AArch64.

Show quoted text 5 lines
On Mar 4, 2023, at 11:17 AM, Dwight Schauer via fpc-pascal wrote: 2022/09/28? Does not do this 3.3.1 [2023/03/02] for me. I get different addresses.

Regards,
Ryan Joseph

Dwight Schauer
to Hairy Pixels: This caused me a nasty debugging confusion, it’s a bug right? In the program below the…
Hairy Pixels wrote
This caused me a nasty debugging confusion, it’s a bug right? In the program below the two parameters passed to TBinding.Create have the same address despite being function pointers to two different class methods. Calling them both call the same method which is clearly not correct. Free Pascal Compiler version 3.3.1 [2022/09/28] for aarch64

I modified the methods in question from your example and called them. They were separate.

This was already addressed.

https://gitlab.com/freepascal.org/fpc/source/-/issues/39981

resolved in commit a714e2ff 3 months ago

Michael Van Canneyt
to Dwight Schauer: I modified the methods in question from your example and called them. They were separate.…
Dwight Schauer wrote 16 lines
On 3/3/23 22:08, Hairy Pixels via fpc-pascal wrote: > This caused me a nasty debugging confusion, it’s a bug right? In the program below the two parameters passed to TBinding.Create have the same address despite being function pointers to two different class methods. Calling them both call the same method which is clearly not correct. > > Free Pascal Compiler version 3.3.1 [2022/09/28] for aarch64 I modified the methods in question from your example and called them. They were separate. This was already addressed. https://gitlab.com/freepascal.org/fpc/source/-/issues/39981 resolved in commit a714e2ff 3 months ago

I can confirm: adding code to call results in different methods being called. Compiler of last week.

Michael.