MethodToProcedure not the same address when called
Posted: Mon Dec 05, 2011 11:05 pm
I am trying to use a class method in the SetTimer call. It seems to work (the procedure is called) however, it is not the same object. The address of "self" is not the same as the address passed into MethodToProcedure. Thus class variables I need access to are invalid.
Similar to - http://stackoverflow.com/questions/2787 ... s-api-call
Similar to - http://stackoverflow.com/questions/2787 ... s-api-call
Code: Select all
TMyClass = class(TObject)
private
fTimer : Cardinal;
SetTimerProc: Pointer;
fMsg : String;
procedure TimerProc();
public
procedure DoIt();
constructor Create(msg : String = 'Hello');
destructor Destroy(); override;
end;
constructor TMyClass.Create(msg : String);
begin
fMsg := msg;
end;
destructor TMyClass.Destroy;
begin
KillTimer(0, fTimer);
VirtualFree(SetTimerProc, 0, MEM_RELEASE);
inherited;
end;
procedure TMyClass.DoIt;
begin
SetTimerProc := SageRiskUtil.MethodToProcedure(Self, @TMyClass.TimerProc);
fTimer := SetTimer(0, 0, 1, SetTimerProc);
ShowMessage('Initial self Addr - ' + IntToStr(Integer(@Self)));
KillTimer(0, fTimer);
end;
procedure TMyClass.TimerProc;
begin
//ShowMessage(IntToStr(Integer(@Self)));
ShowMessage(fMsg + ' TimerProc Self Addr -' + IntToStr(Integer(@Self)));
end;
///// Form Main////////
procedure TForm_Main.Button1Click(Sender: TObject);
var
testClass : TMyClass;
begin
testClass := TMyClass.Create();
testClass.DoIt();
testClass.Free;
end;