Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here


Freeing forms

Freeing forms

Freeing forms

How can i be sure that when i close my application, all the forms are released from memory? I have a few stay on the op forms that are automatically created and some that i create on demand.
It looks like i may have one that is automatically created that may not get free when the application closes.
what i am saying that is that my program.exe still show in the task manager. i found out that because i could not recompile the program after it closed.
I have a menu that show/item the form and I am using form.show and form.close to alternate.

Any help or advice would be appreciated.

RE: Freeing forms

When a form is created it will have an owner. When you let the application create the forms then the owner is the Application. So when Application closes it will free all of the forms.

However, if a form, when being freed, is asking a question or has encountered an error then it might hang around without being able to tell you that there is a problem and therefore stays open.

RE: Freeing forms

There are multiple ways to create a Form (read a TWincontrol):

CODE --> delphi

MyForm := TFormA.Create(Application); // case 1
MyForm := TFormB.Create(FormA); // case 2
MyForm := TFormA.Create(nil); // case 3 

  • in case 1, your form will be freed when the application closes
  • in case 2, your form will be freed the moment FormA will be freed
  • in case 3, your form will not be freed
now there are several ways to free a form:

- use the formclose event and chose caFree as closeaction
- use MyForm.Free

which case you need to use, depends on the situation (SDI, MDI application).
This is what I always do:
  • Autocreate the main form (=CreateForm method in dpr code)
  • All other forms are generated at runtime
  • I delete the autocreated form variable from the unit and I use only class visible variables:
so for example I have 2 forms (form1, form2) where Form1 is the main form, this means that Form1 will hold a private variable form2
it is up to form1 to create form2 and free form2 (using the formcreate and formdestroy methods)
'Dialog' forms are created as a local function variable and then using this pattern:

CODE -->

procedure ShowOptionsDialog;

  Dlg_options : TOptionsDialog; // is a TForm

 Dlg_options := TOptionsDialog.Create(nil);
  // set properties
  Dlg_options.Path := 'c:\temp'; 
  if Dlg_options.Execute then
   // do whatever we need to do


Helping people is my job...

RE: Freeing forms

One more thing,

when your application exits, all memory (even leaked memory) will be returned to the OS, so in the end it does not really matter.
Only if create a lot of objects and you never free them you will get into problems when your application will use up all OS memory.


Helping people is my job...

RE: Freeing forms

Thanks guys for all your explanation. I found out the culprit. I had a variable in a specific form that I had to free before closing the form. The form was not really the issue.
Appreciate your help. It helped me find out the problem.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close