Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Freeing forms 2

Status
Not open for further replies.

pierrotsc

Programmer
Nov 25, 2007
358
US
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.
Thanks.
Pierre
 
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.
 
There are multiple ways to create a Form (read a TWincontrol):

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

[ul]
[li] in case 1, your form will be freed when the application closes[/li]
[li] in case 2, your form will be freed the moment FormA will be freed[/li]
[li] in case 3, your form will not be freed[/li]
[/ul]

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:
[ul]
[li] Autocreate the main form (=CreateForm method in dpr code)[/li]
[li] All other forms are generated at runtime[/li]
[li] I delete the autocreated form variable from the unit and I use only class visible variables:[/li]
[/ul]
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;

var
  Dlg_options : TOptionsDialog; // is a TForm

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

Cheers,
Daddy

-----------------------------------------------------
Helping people is my job...
 
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.

/Daddy

-----------------------------------------------------
Helping people is my job...
 
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.
Pierre
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top