Solved

Form not completely freed

Posted on 2002-03-16
11
236 Views
Last Modified: 2010-04-05
I have a mdi application and I wanted to completely destroy a form instance. I was using the delphi example of setting the action to caFree on the onClose event.
But the form isn't = nil as it is when it isn't even created. Does anybody knows anyway I can completely free it?
0
Comment
Question by:rafaelri
  • 3
  • 3
  • 2
  • +2
11 Comments
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 6872004
Freeing a form doesn't automatically assign 'nil' to the form variable. Actually, after the form is freed, the form variable becomes invalid. It's pointing to something in-memory that isn't there anymore.

Therefore, to free a form, also assign nil after freeing it. Like:
  MyForm.Close;
  MyForm := Nil;

But if it's an MDI application you better don't use a variable. The main form has a property 'mdichildren[Index: integer]' and within this list you can search for your form (e.g. based on it's class or the value of the tag or whatever). You would then say 'MDIChildren[3].Close' to close one of the MDI forms.
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 6872007
Let me repeat this: It's better not to use global variables for MDI forms!
0
 

Author Comment

by:rafaelri
ID: 6872176
I've already tried to set it's variable to nil but it doesn't seem to work. By the way, it's a modeless window, so I need to code it on the unit of the form.
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

 

Author Comment

by:rafaelri
ID: 6872208
The way you said works, but not for my problem. I need to free it coding on its unit. I've tried to set it to nil at the ondestroy event... and lots of other ways... any suggestions?
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6872802
Surely once you called close with Action := caFree in the OnClose event handler, it's gone!
MyForm = meaningless.
Try using memory sleuth or similar to see if you freed everything.
0
 
LVL 44

Accepted Solution

by:
CrazyOne earned 50 total points
ID: 6873481
Here is how I did it and From2 returns nil everytime

procedure TForm1.Button1Click(Sender: TObject);
begin

  if Form2 = nil then
    Form2 := TForm2.Create(Nil);
  Form2.Show;

end;


procedure TForm2.Button1Click(Sender: TObject);
begin

  Close;

end;


procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);
begin

  FreeAndNil(Form2);

end;


The Crazy One
0
 
LVL 4

Expert Comment

by:jsweby
ID: 6878408
I have to agree with Workshop_Alex here, assigning nil to the form variable works, it works for me in exactly this situation.

In the FormClose event:

Action := caFree;
Form2 := nil;

That's it! Your test in the Button1Click procedure should now evaluate correctly.

J.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6878506
if Form2 = nil then.. is wrong. If you freed form2 earlier then the variable form2 is gone.

Simple to do:
When you create form2 set a global (i'm ducking!) variable like Form2Exists: Boolean; in the create event.
When you destroy form2, unset that global in the destroy event.

now to say:
if not Form2Exists then.. at least is ok and reliable.
0
 
LVL 3

Expert Comment

by:SteveWaite
ID: 6878540
maybe
Form2.Release;
will be of help.

To free a form, call its Release method, which destroys the form and releases the memory allocated for it after all its event handlers and those of the components it contains are through executing.
0
 
LVL 44

Expert Comment

by:CrazyOne
ID: 6878774
FreeAndNil(Form2); Works just fine and you only need one line of code to accomplish it. :>)
0
 

Author Comment

by:rafaelri
ID: 6892171
That's the way I did, I found the problem, I was instancing another variable. I think that it was referencing some components that weren't freed at all. But as I only wanted an instance I'm using the Form2 Global variable and it's working.
0

Featured Post

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

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…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

828 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