Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

What is the better option to destroy a form?

Posted on 2006-06-19
11
Medium Priority
?
664 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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 400 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 400 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 800 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: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

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

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…
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…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

730 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