Solved

What is the better option to destroy a form?

Posted on 2006-06-19
11
647 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
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 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

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

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…
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…
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 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

16 Experts available now in Live!

Get 1:1 Help Now