Recent

Author Topic: [SOLVED] Sender object problem  (Read 916 times)

MaartenJB

  • Jr. Member
  • **
  • Posts: 73
[SOLVED] Sender object problem
« on: January 29, 2020, 12:26:44 pm »
Hi, I've got a problem with the sender object. I want to store the sender as a reference. If I try this with the OnClick event it works, but if I try it with for example the OnMouseDown or OnResize event it doesnt work. For some reason this is handled differently, maybe in the OnMouseDown the Sender object is a copy of the original sender and is freed after the event is done?

Reproduce:

Click on button1 > Click on Button3 > Message is shown with "TButton"
Click on button2 > Click on Button3 > Exception is shown

Code: Pascal  [Select][+][-]
  1. var
  2.   ObjectTest : ^TObject;
  3.  
  4. procedure TForm1.Button1Click(Sender: TObject);
  5. begin
  6.   ObjectTest := @Sender; // Works
  7. end;
  8.  
  9. procedure TForm1.Button2MouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  10. begin
  11.   ObjectTest := @Sender; // Does not work
  12. end;
  13.  
  14. procedure TForm1.Button3Click(Sender: TObject);
  15. begin
  16.   // when Button1Click it returns "TButton"
  17.   // when Button2MouseDown it returns an exception
  18.   ShowMessage(ObjectTest^.ClassName);
  19. end;  
  20.  
« Last Edit: January 31, 2020, 10:24:16 am by MaartenJB »

HeavyUser

  • Sr. Member
  • ****
  • Posts: 310
Re: Sender object problem
« Reply #1 on: January 29, 2020, 01:14:05 pm »
well remove the pointers and derefence and just use tobject directly they are typed pointers after all.

Bart

  • Hero Member
  • *****
  • Posts: 3943
    • Bart en Mariska's Webstek
Re: Sender object problem
« Reply #2 on: January 29, 2020, 01:33:08 pm »
Sender might be nil?

Bart

MaartenJB

  • Jr. Member
  • **
  • Posts: 73
Re: Sender object problem
« Reply #3 on: January 29, 2020, 01:39:56 pm »
@HeavyUser It works indeed and exactly like you said, thanks for that insight. But my code should work as well right? I use pointers in other projects and would be interested to know why this is not working so I know when to avoid this.

@Bart Sender is not nil

MaartenJB

  • Jr. Member
  • **
  • Posts: 73
Re: Sender object problem
« Reply #4 on: January 29, 2020, 01:56:24 pm »
@HeavyUser Just gonna use your proposal in the future when I want to reference to an object, it works as stated and is more clean in code as well.

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 6675
  • Debugger - SynEdit - and more
    • wiki
Re: Sender object problem
« Reply #5 on: January 29, 2020, 02:35:43 pm »
Code: Pascal  [Select][+][-]
  1.  @Sender;
Will give you a pointer to the local variable "Sender".  (That is an address somewhere on the stack)

That variable goes out of scope at the end of the procedure. After that the pointer points to memory with random content. (And such a pointer should not be used/dereferenced any more).



MaartenJB

  • Jr. Member
  • **
  • Posts: 73
Re: Sender object problem
« Reply #6 on: January 31, 2020, 10:23:48 am »
Thanks all for helping me out!

 

TinyPortal © 2005-2018