Solved

Self destruction

Posted on 2000-03-26
13
420 Views
Last Modified: 2010-08-05
Hi

Suppose I have a frame in a panel on a form. It displays properties of a given object. Now I want to put a button in that frame that will create another frame in the same place (i.e. Parent) which subsequently causes it's own destruction.

How can I do this without causing random access violations?

Have fun,

Edo
0
Comment
Question by:Edo082297
13 Comments
 
LVL 13

Expert Comment

by:Epsylon
ID: 2658760
An object can't destroy itself...
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 2658970
Actually, an object can destroy itself. You just need to make sure that the object that does free itself never references a data member of the class after freeing itself.

eg:

  procedure TSomeObject.DoSomething;
  begin
    FFred := 0; // FFred is data member: OK
    Free; // Destroy this instance
    ShowMessage('I''m dead');
    FFred := 0; // Bad, this will AV.
  end;

Cheers,

Raymond.
0
 

Expert Comment

by:sunsetyang
ID: 2659335
You can either use a global variable to be sure of its in life when want to access.When it is freed,you should set the variable to mark this class has been destructed.If you are sure of your code,You can remove the flag and test for the proper result.
0
 
LVL 1

Expert Comment

by:yk030299
ID: 2659417
don't destroy when construction
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2659865
From Delphi help:

Warning: Never explicitly free a component within one of its own event handlers or free a component from the event handler of a component it owns or contains. For example, don’t free a button in its OnClick event handler or free the form that owns the button from the button's OnClick event.


So what I said is, more or less, right...
0
 
LVL 1

Author Comment

by:Edo082297
ID: 2659994
Fine: it can't be done.

Post an answer, Epslyon, I suppose.

Raymond, I asked specifically about the button case, but thanks anyway.

I was really hoping someone knew an elegant way to unwind the stack or clear the message queue  such that control wouldn't return to the handler: but I believe this is not possible, as the button handler's address has been pushed into a register as the control return.

Cheers

Edo
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 3

Expert Comment

by:Slavak
ID: 2660063
If you call Close method of the form from button onClick event you actually destroy ( free ) the form. Difference is that close method use PostMessage function to be sure that all data member use over.
From my view Raymond answer should gain the points.

Cheers.

0
 
LVL 20

Expert Comment

by:Madshi
ID: 2660208
Look in the VCL sources at the implementation of the TForm.Release method. You can use the same logic for a button, too. Then the button *CAN* destroy itself without danger.

Regards, Madshi.
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2660215
There is a solution. Instead of 'Free' send a user defined message to the main form:


PostMessage(Application.MainForm.Handle, WM_USER+1, 0, 0);

The main unit will receive the message and calls 'Free' and creates the new frame.


type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
    procedure WMUSER(var Message: TWMDestroy); message WM_USER+1;
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  F1, F2: TFrame;

implementation

uses Unit2, Unit3;

{$R *.DFM}

procedure TForm1.Button1Click(Sender: TObject);
begin
  F1 := TFrame2.Create(Self);
  F1.Align := alClient;
  F1.Parent := Panel1;
end;

procedure TForm1.WMUSER(var Message: TWMDestroy);
begin
  F1.Free;
  F2 := TFrame3.Create(Self);
  F2.Align := alClient;
  F2.Parent := Panel1;
end;
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2669265
Edo, does this solve your problem?
0
 
LVL 13

Expert Comment

by:Epsylon
ID: 2677085
Edo?
0
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 2688178
>LOCK<
0
 
LVL 1

Author Comment

by:Edo082297
ID: 2698212
Thankyou, this is good.

Cheers

Edo
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
Internet Business Fax to Email Made Easy - With  eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, f…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now