fpc/tests/webtbs/tw26177.pp
svenbarth 441e6c6083 Fix for Mantis #26177. Use the correct value for the method data when using a method pointer of a type or record helper.
Please note that Delphi has the exact same bug as we have and thus code working in FPC will not work in Delphi.
Additionally taking the method address of a local variable or a local/global constant for a method pointer is dangerous as the variable (in case of constants is a temporary local variable) will go out of scope once the containing procedure/function/method exits!

ncgld.pas, tcgloadnode.pass_generate_code:
  * only use the value of Self if it is an implicit pointer object (class instance) or a class reference, but not for everything else (objects, records, primitive types)

+added test

git-svn-id: trunk@28160 -
2014-07-05 08:54:10 +00:00

39 lines
577 B
ObjectPascal

program tw26177;
{$MODE DELPHI}
{$MODESWITCH TYPEHELPERS}
uses
Classes;
type
TInt32Helper = record helper for Int32
procedure Foo(Sender: TObject);
end;
var
value: Int32 = 0;
procedure TInt32Helper.Foo(Sender: TObject);
begin
value := Self;
end;
var
i: Int32 = 10;
m: TNotifyEvent;
begin
m := i.Foo;
// Data is equal 10 (!) but should be equal to @i
//WriteLn(Int32(TMethod(m).Data));
// TMethod(m).Data := @i; < workaround for bug
try
m(nil); // External SIGSEGV!
if value <> 10 then
Halt(2);
except
Halt(1);
end;
end.