Solved

What is the better option to destroy a form?

Posted on 2006-06-19
11
652 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

 
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
 
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Machine not responding during CopyFile() 3 104
Magic Software info 18 138
Delphi: barcode reading on android platform 1 51
Slow Restore if incremental backups using RDiff.exe 4 30
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

856 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