Solved

What is the better option to destroy a form?

Posted on 2006-06-19
11
660 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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
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…
In this video, viewers will be given step by step instructions on adjusting mouse, pointer and cursor visibility in Microsoft Windows 10. The video seeks to educate those who are struggling with the new Windows 10 Graphical User Interface. Change Cu…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month6 days, 13 hours left to enroll

622 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