Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Form not completely freed

Posted on 2002-03-16
11
Medium Priority
?
239 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
[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
  • +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
Independent Software Vendors: 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!

 

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 150 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

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

722 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