Solved

Form not completely freed

Posted on 2002-03-16
11
233 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
 

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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now