Solved

Form not completely freed

Posted on 2002-03-16
11
238 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
[Live Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

 

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

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…
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…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month6 days, 5 hours left to enroll

626 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