Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

MyForm := nil;

_yilmaz_yilmaz Posts: 150Member

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?


Comments

  • zibadianzibadian Posts: 6,349Member
    :
    : 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:
    [code]
    procedure TForm2.OnClose(Sender: TObject; var Action: TCloseAction);
    begin
    Action := caFree; // free the form instead of minimizing it.
    end;
    [/code]
    Because the form will remain in the memory, you will have a great chance of running out of memory.
  • _yilmaz_yilmaz Posts: 150Member
    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:
    : [code]
    : procedure TForm2.OnClose(Sender: TObject; var Action: TCloseAction);
    : begin
    : Action := caFree; // free the form instead of minimizing it.
    : end;
    : [/code]
    : Because the form will remain in the memory, you will have a great chance of running out of memory.
    :

  • zibadianzibadian Posts: 6,349Member
    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:
    : : [code]
    : : procedure TForm2.OnClose(Sender: TObject; var Action: TCloseAction);
    : : begin
    : : Action := caFree; // free the form instead of minimizing it.
    : : end;
    : : [/code]
    : : Because the form will remain in the memory, you will have a great chance of running out of memory.
    : :
    :
    :

Sign In or Register to comment.