Recent

Author Topic: [SOLVED] List of TNotifyEvent?  (Read 9124 times)

Dibo

  • Hero Member
  • *****
  • Posts: 1048
[SOLVED] List of TNotifyEvent?
« on: August 31, 2010, 09:05:29 pm »
Hi,

How can I create list of procedures TNotifyEvent? I want collect pointers to TNotifyEvent procedures of other objects and then call this events from first object. Example of what I want get:
Code: Pascal  [Select][+][-]
  1. type
  2.   TSomeObject = class
  3. private
  4.   procedure OnSomeEvent(Sender: TObject);
  5.   procedure OnButtonClick(Sender: TObject);
  6. end;
  7.  
  8. type
  9.   TOtherObject = class
  10.   private
  11.     FHookList: TList;
  12.   public
  13.     constructor Create;
  14.     destructor Destroy; override;
  15.    
  16.     procedure RegisterEvent(Event: TNotifyEvent);
  17.     procedure UnregisterEvent(Event: TNotifyEvent);
  18.     procedure CallEvents;
  19. end;
  20.  
  21. procedure TSomeObject.OnSomeEvent(Sender: TObject);
  22. begin
  23.   // Some code here
  24. end;
  25.  
  26. procedure TSomeObject.OnButtonClick(Sender: TObject);
  27. begin
  28.   OtherObject.RegisterEvent(@OnSomeEvent);
  29. end;
  30.  
  31. constructor TOtherObject.Create;
  32. begin
  33.   inherited Create;
  34.  
  35.   FHookList := TList.Create;
  36. end;
  37.  
  38. destructor TOtherObject.Destroy;
  39. begin
  40.   FHookList.Free;
  41.   inherited Destroy;
  42. end;
  43.  
  44. procedure TOtherObject.RegisterEvent(Event: TNotifyEvent);
  45. begin
  46.   FHookList.Add(@Event);
  47. end;
  48.  
  49. procedure TOtherObject.CallEvents;
  50. var
  51.   i: Integer;
  52.   Event: TNotifyEvent;
  53. begin
  54.   for i:=0 to Pred(FHookList.Count) do
  55.   begin
  56.     Event := TNotifyEvent(FHookList.Items[i]^);
  57.     Event(Self);
  58.   end;
  59. end;
  60.  

But when I call Event(Self); in procedure CallEvents I get SIGSEGV error
« Last Edit: September 01, 2010, 10:25:52 pm by Dibo »

Marc

  • Administrator
  • Hero Member
  • *
  • Posts: 2580
Re: List of TNotifyEvent?
« Reply #1 on: September 01, 2010, 04:41:55 pm »
Code: Pascal  [Select][+][-]
  1.  procedure TOtherObject.RegisterEvent(Event: TNotifyEvent);  
  2.  begin  
  3.    FHookList.Add(@Event);  
  4.  end;  
  5.  

There you go.... A TNotifyEvent is not a pointer, it is a "record" with a Code and data pointer.
What you add to FHookList is the address if this record, located somewhere on stack, which is void after you leave RegisterEvent

Use something like:

Code: Pascal  [Select][+][-]
  1.  type
  2.    PNotifyEvent = ^TNotifyEvent;
  3.  
  4.  procedure TOtherObject.RegisterEvent(Event: TNotifyEvent);  
  5.  var
  6.    p: PNotifyEvent;
  7.  begin  
  8.    New(p);
  9.    p^ := Event;
  10.    FHookList.Add(p);  
  11.  end;  
  12.  

when clearing or removing items from the list, call Dispose(p);


//--
{$I stdsig.inc}
//-I still can't read someones mind
//-Bugs reported here will be forgotten. Use the bug tracker

Dibo

  • Hero Member
  • *****
  • Posts: 1048
Re: List of TNotifyEvent?
« Reply #2 on: September 01, 2010, 10:24:25 pm »
<beer> ;) . I thought that TNotifyEvent is exactly pointer to the procedure. Thanks

 

TinyPortal © 2005-2018