Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 240
  • Last Modified:

Form not completely freed

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
rafaelri
Asked:
rafaelri
  • 3
  • 3
  • 2
  • +2
1 Solution
 
Wim ten BrinkCommented:
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
 
Wim ten BrinkCommented:
Let me repeat this: It's better not to use global variables for MDI forms!
0
 
rafaelriAuthor Commented:
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!

 
rafaelriAuthor Commented:
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
 
SteveWaiteCommented:
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
 
CrazyOneCommented:
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
 
jswebyCommented:
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
 
SteveWaiteCommented:
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
 
SteveWaiteCommented:
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
 
CrazyOneCommented:
FreeAndNil(Form2); Works just fine and you only need one line of code to accomplish it. :>)
0
 
rafaelriAuthor Commented:
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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 3
  • 3
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now