Delphi and Kylix

Moderators: pritaeas
Number of threads: 7244
Number of posts: 19051

This Forum Only
Post New Thread
Single Post View       Linear View       Threaded View      f

Report
MyForm := nil; Posted by _yilmaz on 2 Oct 2003 at 4:26 AM

Hi all;

In an MDI application, I create such a child form when a button on the main MDIform is clicked:

var
MyForm : TForm2;
begin
MyForm := TForm2.Create(self)
end;

and in the OnClose event of TForm2 I write:

begin
Self := nil
end;

what would be the difference if i wrote "Self.Release" instead of
"Self := nil" ? If I assign the form to nil, does it still reside in memory as garbage? I mean is there any performance degradation regarding memory?


Report
Re: MyForm := nil; Posted by zibadian on 2 Oct 2003 at 7:18 AM
:
: Hi all;
:
: In an MDI application, I create such a child form when a button on the main MDIform is clicked:
:
: var
: MyForm : TForm2;
: begin
: MyForm := TForm2.Create(self)
: end;
:
: and in the OnClose event of TForm2 I write:
:
: begin
: Self := nil
: end;
:
: what would be the difference if i wrote "Self.Release" instead of
: "Self := nil" ? If I assign the form to nil, does it still reside in memory as garbage? I mean is there any performance degradation regarding memory?
:
If you assign the self to nil, the form will continue to exist as garbage. You can better use the following code to use the "official" method of freeing a MDIchild:
procedure TForm2.OnClose(Sender: TObject; var Action: TCloseAction);
begin
  Action := caFree; // free the form instead of minimizing it.
end;

Because the form will remain in the memory, you will have a great chance of running out of memory.
Report
Re: MyForm := nil; Posted by _yilmaz on 3 Oct 2003 at 4:28 AM
Thanx Zibadian. But I have something to ask here:

Assume that I have a global variable like this:

TempForm : TForm2;

and just before closing the child form, I assign it to TempForm, as the following code shows:

procedure TForm2.OnClose (Sender:Tobject);
begin
TempForm := Self;
Self := nil
end;

And in the OnClick event of some button on the main form, I write this:

TempForm.Show;

Now, after closing the child form, I click on this button but nothing happens. Shouldn't that child form appear again on the screen? Because I think there is a variable (TempForm) which still refers to the data of the child form residing in memory. Do I think wrong? Because in C, the pointer mechanism just works like this.

(Moreover, I follow the memory allocation in the task manager. When the child form is closed by assigning it to nil, the memory is deallocated as far as I can see.)

Thanx again,
Yilmaz

: :
: : Hi all;
: :
: : In an MDI application, I create such a child form when a button on the main MDIform is clicked:
: :
: : var
: : MyForm : TForm2;
: : begin
: : MyForm := TForm2.Create(self)
: : end;
: :
: : and in the OnClose event of TForm2 I write:
: :
: : begin
: : Self := nil
: : end;
: :
: : what would be the difference if i wrote "Self.Release" instead of
: : "Self := nil" ? If I assign the form to nil, does it still reside in memory as garbage? I mean is there any performance degradation regarding memory?
: :
: If you assign the self to nil, the form will continue to exist as garbage. You can better use the following code to use the "official" method of freeing a MDIchild:
:
: procedure TForm2.OnClose(Sender: TObject; var Action: TCloseAction);
: begin
:   Action := caFree; // free the form instead of minimizing it.
: end;
: 

: Because the form will remain in the memory, you will have a great chance of running out of memory.
:

Report
Re: MyForm := nil; Posted by zibadian on 3 Oct 2003 at 5:45 AM
You should check if TempForm = nil. I think it will be, because Self always refers to the instance of the form. So if you assign nil to self, that means that the instance of the form is nil, and thus the TempForm is also nil.

: Thanx Zibadian. But I have something to ask here:
:
: Assume that I have a global variable like this:
:
: TempForm : TForm2;
:
: and just before closing the child form, I assign it to TempForm, as the following code shows:
:
: procedure TForm2.OnClose (Sender:Tobject);
: begin
: TempForm := Self;
: Self := nil
: end;
:
: And in the OnClick event of some button on the main form, I write this:
:
: TempForm.Show;
:
: Now, after closing the child form, I click on this button but nothing happens. Shouldn't that child form appear again on the screen? Because I think there is a variable (TempForm) which still refers to the data of the child form residing in memory. Do I think wrong? Because in C, the pointer mechanism just works like this.
:
: (Moreover, I follow the memory allocation in the task manager. When the child form is closed by assigning it to nil, the memory is deallocated as far as I can see.)
:
: Thanx again,
: Yilmaz
:
: : :
: : : Hi all;
: : :
: : : In an MDI application, I create such a child form when a button on the main MDIform is clicked:
: : :
: : : var
: : : MyForm : TForm2;
: : : begin
: : : MyForm := TForm2.Create(self)
: : : end;
: : :
: : : and in the OnClose event of TForm2 I write:
: : :
: : : begin
: : : Self := nil
: : : end;
: : :
: : : what would be the difference if i wrote "Self.Release" instead of
: : : "Self := nil" ? If I assign the form to nil, does it still reside in memory as garbage? I mean is there any performance degradation regarding memory?
: : :
: : If you assign the self to nil, the form will continue to exist as garbage. You can better use the following code to use the "official" method of freeing a MDIchild:
: :
: : procedure TForm2.OnClose(Sender: TObject; var Action: TCloseAction);
: : begin
: :   Action := caFree; // free the form instead of minimizing it.
: : end;
: : 

: : Because the form will remain in the memory, you will have a great chance of running out of memory.
: :
:
:




 

Recent Jobs

Official Programmer's Heaven Blogs
Web Hosting | Browser and Social Games | Gadgets

Popular resources on Programmersheaven.com
Assembly | Basic | C | C# | C++ | Delphi | Flash | Java | JavaScript | Pascal | Perl | PHP | Python | Ruby | Visual Basic
© Copyright 2011 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Operated by CommunityHeaven, a BootstrapLabs company.