Board index » cppbuilder » How to instantiate a Form at runtime

How to instantiate a Form at runtime

After removing a form (let's say Form2) from the project's "Auto-Created
Forms",
to instantiate dynamically the form at runtime, is it better to write
(in the main
form unit) :

TForm2 *form2=new TForm2(this) ;
... etc
delete form2 ;

or

TForm2 *form2=new TForm2(NULL) ;
... etc
delete form2 ;

I thought it was better to give the form no Owner (NULL), until I read
a program where they tranmitted the Main Form as the owner.

Michel Corbin

 

Re:How to instantiate a Form at runtime


Quote
"Michel Corbin" <corbin.mic...@libertysurf.fr> wrote in message

news:3EB563FA.F79F5FF9@libertysurf.fr...

Quote
> is it better to write (in the main form unit) :

> TForm2 *form2=new TForm2(this) ;

> or

> TForm2 *form2=new TForm2(NULL) ;

They are both the same, as far as the allocate itself goes.  The only
difference between them is that the first one assigns an Owner to the form,
whereas the second one doesn't.  The Owner is responsible for freeing the
object when the Owner itself is freed.  This is good practice, in case your
own code doesn't explicit 'delete' the object first for whatever reason, the
Owner will ensure the object still gets freed properly later on so the
memory isn't leaked.

Gambit

Re:How to instantiate a Form at runtime


"Remy Lebeau (TeamB)" wrote :

Quote

> "Michel Corbin" <corbin.mic...@libertysurf.fr> wrote in message
> news:3EB563FA.F79F5FF9@libertysurf.fr...
> > is it better to write (in the main form unit) :

> > TForm2 *form2=new TForm2(this) ;

> > or

> > TForm2 *form2=new TForm2(NULL) ;

> They are both the same, as far as the allocate itself goes.  The only
> difference between them is that the first one assigns an Owner to the form,
> whereas the second one doesn't.  The Owner is responsible for freeing the
> object when the Owner itself is freed.  This is good practice, in case your
> own code doesn't explicit 'delete' the object first for whatever reason, the
> Owner will ensure the object still gets freed properly later on so the
> memory isn't leaked.

> Gambit

Thanks, so I can always write :
TForm2 *form2=new TForm2(this) ;
// and later :
delete form2 ;

in fact I was afraid that doing so, the Owner could attempt to free
something
already freed by "delete form2 ;"

Michel Corbin

Re:How to instantiate a Form at runtime


Quote
"Michel Corbin" <corbin.mic...@libertysurf.fr> wrote in message

news:3EB65C9B.CAF5026C@libertysurf.fr...

Quote

> Thanks, so I can always write :
> TForm2 *form2=new TForm2(this) ;
> // and later :
> delete form2 ;

Have a look in the help files for Release().

From what I can make out it seems to be the preferred method for freeing
memory associated with Forms.

form2->Release();

Re:How to instantiate a Form at runtime


Quote
"Michel Corbin" <corbin.mic...@libertysurf.fr> wrote in message

news:3EB65C9B.CAF5026C@libertysurf.fr...

Quote
> in fact I was afraid that doing so, the Owner could
> attempt to free something already freed by "delete form2 ;"

Nope.  When an owned component is freed, whether automatically or
explicitially, it removes itself from its Owner's internal list so that the
Owner won't try to free it again later.

Gambit

Re:How to instantiate a Form at runtime


Hi, Michael!

Always remember when you have to manually free something, to make sure
you use either a smart pointer or try/__finally to ensure the resource
is deleted, even if you experience an error.

--
------
Mark Cashman (TeamB - C++ Builder), creator of The Temp{*word*203}Doorway at
http://www.temporaldoorway.com
(original digital art, writing, music and more)
C++ Builder / JBuilder Tips and The C++ Builder Programmer's Webring
(Join us!)
http://www.temporaldoorway.com/programming/index.htm
------

Other Threads