Deleting Forms

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

Deleting Forms

Postby smd » Thu Oct 29, 2015 10:48 am

Using Firemonkey Rad 10

I am not clear about an issue with forms. Say I have a form called MyForm. To open it I use the following code

Code: Select all
if (!MyForm)
  MyForm = new TMyForm(this);
MyForm->Show();


My question is: When I terminate the program, is the destructor automatically called? or do I have to specifically add something like

Code: Select all
delete MyForm;


when the main form is destroyed.

I use a few TStringList components which are created at run-time then delete on the FormDestroy event of the main form. My understanding is that forms though are automatically deleted.

If I am wrong on this, do I have to add a delete for each form created when the program terminates? or will I have a memory leak by not doing so?
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas

Re: Deleting Forms

Postby rlebeau » Thu Oct 29, 2015 8:02 pm

smd wrote:When I terminate the program, is the destructor automatically called?


Yes, when 'this' is destroyed, because you assigned 'this' as the Form's Owner. Had you assigned a NULL Owner instead, then you would have to destroy the Form manually.

These rules apply to *any* class that derives from TComponent.

smd wrote:do I have to specifically add something like

Code: Select all
delete MyForm;


when the main form is destroyed.


You can, but it is not necessary if the Form has an Owner assigned.

smd wrote:I use a few TStringList components which are created at run-time then delete on the FormDestroy event of the main form.


TStringList is not an Ownable component class, so you have to delete it manually.

And BTW, DO NOT use the Form's OnCreate and OnDestroy events in C++. They are Delphi idioms that can produce illegal behavior in C++. Use the Form's actual constructor and destructor instead.

smd wrote:My understanding is that forms though are automatically deleted.


They will be if they have an Owner assigned.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1463
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: Deleting Forms

Postby smd » Fri Oct 30, 2015 12:21 am

If not the FormCreate and FormDestroy, event, then what am I supposed to use?
-----------------------------
Scott
smd
BCBJ Guru
BCBJ Guru
 
Posts: 130
Joined: Sat Nov 29, 2014 8:02 pm
Location: Las Vegas

Re: Deleting Forms

Postby rlebeau » Fri Oct 30, 2015 8:34 pm

smd wrote:If not the FormCreate and FormDestroy, event, then what am I supposed to use?


I told you what to use instead:

rlebeau wrote:Use the Form's actual constructor and destructor instead.


You know, those special methods that have the same name as the class itself:

Code: Select all
__fastcall <Classname>(TComponent *Owner);
__fastcall ~<Classname>();


So, if your form class is TMyForm, the constructor is TMyForm() and the destructor is ~TMyForm().

Every C++ developer needs to know what a constructor and destructor are.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1463
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: Lena and 12 guests