Articles   Members Online:
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
How to free a component in a message handler Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
30-Aug-02
Category
Win API
Language
Delphi 2.x
Views
46
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: Tomas Rutkauskas 

Is it possible to free a component inside its own event handler?

Answer:

Not safely. You never know what the code after your free statement will still try 
to do with the (now invalid) self reference.

Do it the way forms implement the Release method: post a user message to the form, 
passing the control to delete as parameter, then free the control in the message 
handler:


1   const
2     UM_DELETEOBJECT = WM_USER + 666;
3   type
4     TUMDeleteObject = packed record
5       Msg: Cardinal;
6       Obj: TObject;
7       Unused: Longint;
8       Result: Longint;
9     end;
10  
11    {in form declaration, private section}
12  
13  procedure UMDeleteObject(var msg: TUMDeleteObject); message UM_DELETEOBJECT;
14  
15  procedure TaaDEOutputFrm.UMDeleteObject(var msg: TUMDeleteObject);
16  begin
17    msg.Obj.Free;
18  end;
19  
20  procedure TaaDEOutputFrm.PanelClick(Sender: TObject);
21  begin
22    if Sender is TPanel then
23      PostMessage(handle, UM_DELETEOBJECT, wparam(sender), 0);
24  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC