Solved

What is the better option to destroy a form?

Posted on 2006-06-19
11
645 Views
Last Modified: 2010-04-05
This way:

Form1 := Tform1.Create(Application);
Try
  Form1.ShowModal;
Finally
  Form1.Release
End;

This way?
Form1 := Tform1.Create(Application);
Try
  Form1.ShowModal;
Finally
  Form1.free
  Form1 := Nil
End;

This way?
Form1 := Tform1.Create(Application);
Try
  Form1.ShowModal;
Finally
  FreeAndNil(Form1)
End;



0
Comment
Question by:hidrau
  • 3
  • 3
  • 2
  • +3
11 Comments
 
LVL 26

Expert Comment

by:Russell Libby
ID: 16938443
According to Borland, its the one that uses Release:

Form1 := Tform1.Create(Application);
Try
  Form1.ShowModal;
Finally
  Form1.Release
End;

From the help:

procedure Release;

Description

Use Release to destroy the form and free its associated memory.

Release does not destroy the form until all event handlers of the form and event handlers of components on the form have finished executing. Any event handlers of the form should use Release instead of Free. Failing to do so could lead to an access violation.

---
Regards,
Russell
0
 
LVL 13

Accepted Solution

by:
BlackTigerX earned 100 total points
ID: 16939649
that is not an event handler though...

anyway, Release should work fine anyway (it sends a message to the form, that message calls the .Free method), I would use something like

with TForm1.Create(nil) do //instead of passing Application as the parameter
try
  ShowModal();
finally
  Free;
end;

0
 
LVL 5

Expert Comment

by:ewilde
ID: 16939705
rllibby,

i discovered this direction by borland a few years ago, after using for years the Form1.Free method. i never had any bugs related to this issue. (i'm not sure what version of delphi the release method first apeared).

i guess the only case it's realy important to use Form1.Release is when you try to destroy a form inside an event handler of the form.

so it's perfectly safe to do:

Form1 := Tform1.Create(Application);
Try
  Form1.ShowModal;
Finally
  Form1.Free;
End;

could you describe/deliver an application that produce an access violation because of using Free instead of Release?
please, correct me if i'm wrong.


Regards,
ewilde.
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 16939819
I use like this.

  try
    Form2 := TForm2.create(self);
    Form2.showmodal;
  finally
    Form2.free;
  end;

What ever way you generate a form dont forget to move the form from(except the main form)

Project>Options> Forms  Move from left to right.

This will not load the forms when the applicaiton starts. It will save the memory.
Only then you call the the form , then only it will get create.
 

sun4sunday
0
 
LVL 3

Expert Comment

by:KyleyHarris
ID: 16940730
sun4sunday
>try
>    Form2 := TForm2.create(self);
>    Form2.showmodal;
>finally
>    Form2.free;
>end;

you need to work on your programming technique.

    Form2 := TForm2.create(self);
  try
    Form2.showmodal;
  finally
    Form2.free;
  end;

if you put the create inside your try clause, and the create statement creates an exception then your code handler is going to go straight to the finally and call free on an uninitialized object, which will then hide the intial exception and cause a random AV at some point....

0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 9

Assisted Solution

by:sun4sunday
sun4sunday earned 100 total points
ID: 16941105
The form2 is not exisiting, on the time of compiling itself  delphi will through an error  'Undecleared Identifier'
Without exisitng the form and trying to create it will throw an error.

Nil, Self, Application explained here
http://delphi.about.com/od/adptips2005/qt/nilselfapp.htm

sun4sunday

0
 
LVL 3

Assisted Solution

by:KyleyHarris
KyleyHarris earned 100 total points
ID: 16941225
you are trying to put an interesting light on the fact that I am pointing out that you don't try and create objects inside a try.finally that you free in the finally..end. :D

Do as you will.

I'm pretty certain that you are not clear on what I, or yourself are saying.

Your own link clearly shows the correct way of using try finally

var
   myForm : TMyForm;
begin
   myForm := TMyForm.Create(nil) ;
   try
     myForm.ShowModal;
   finally
     myForm.Free;
   end;
end;

anyway, it has nothing to do with the actual Q. Perhaps you just made a typo
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16941334
as another option, I tend to use a class function/procedure to do it all for me

class procedure TForm2.execute;
begin
  Form2:= TForm2.Create(nil);
  with frmInvoices do
    try
      ShowModal;
    finally
      FreeAndNil(Form2)
    end;
end;

then from the calling form (make sure second unit declared in the USES section of first unit)

procedure TForm1.Button1Click(Sender: TObject);
begin
  TForm2.execute;
end;
0
 
LVL 15

Expert Comment

by:mikelittlewood
ID: 16941337
Sorry that should have said

  with Form2do
    try
      ShowModal;
    finally
      FreeAndNil(Form2)
    end;
0
 
LVL 9

Expert Comment

by:sun4sunday
ID: 16941794
KyleyHarris,
Your argument is also  right. I noticed it in the link what they mentioed.
I confess that i was using in that way for all the time(but never encouter any problems ;) )

Thanks

sun4sunday
0
 
LVL 15

Assisted Solution

by:mikelittlewood
mikelittlewood earned 200 total points
ID: 16943034
I suppose if you wanted to be really picky then you could wrap it in a try except too

class procedure TForm2.execute;
begin
  try
    Form2:= TForm2.Create(nil);
    with Form2 do
      try
        ShowModal;
      finally
        FreeAndNil(Form2)
      end;
  except
    // handle creation exception or pass back to calling form
  end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  TForm2.execute;
end;
0

Featured Post

What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

708 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

19 Experts available now in Live!

Get 1:1 Help Now