Board index » cppbuilder » when to assign NULL to pointer

when to assign NULL to pointer


2005-01-11 01:48:08 AM
cppbuilder10
Hi all,
I have a pointer to mdi child form.
let's say
TmyMDIChild *myPointer=new TmyMDIChild(this);
and then
on OnClose event I have
Action=caFree;
but I as well need to asign to myPointer=NULL;
at the moment when mdichild is closed...
when can I do that?
in OnClose event as well???
or in destructor of mdi child is better???
thanks in advance
 
 

Re:when to assign NULL to pointer

George wrote:
Quote
but I as well need to asign to myPointer=NULL;
at the moment when mdichild is closed...
when can I do that?
in OnClose event as well???
or in destructor of mdi child is better???
Both are ok. If the application is terminated by closing
the mainform then OnClose of MDI children often does not fire
where OnCloseQuery always fires.
But not nulling a pointer where the application terminates
does not matter.
Hans.
 

Re:when to assign NULL to pointer

"George" < XXXX@XXXXX.COM >wrote in message
Quote
I have a pointer to mdi child form.
let's say
TmyMDIChild *myPointer=new TmyMDIChild(this);

and then
on OnClose event I have
Action=caFree;

but I as well need to asign to myPointer=NULL;
at the moment when mdichild is closed...
when can I do that?
Unless you actually use the pointer after creating the form instance, you
don't need to set it to NULL. Especially if you are using the same pointer
to create multiple form instances. The code you showed is using the pointer
as a local variable, so chances are it won't even be in scope anymore by the
time the form is closed.
Gambit
 

{smallsort}

Re:when to assign NULL to pointer

thanks for reply...
the code that I showed doesn't really show the real code!
it was just for a question.
and I know I don't need to set it to NULL..
I just need in other part of a program to check if that form is created,
and thought that would be the best way to know it.
thanks for reply to everone!
"Remy Lebeau (TeamB)" < XXXX@XXXXX.COM >wrote in message
Quote

"George" < XXXX@XXXXX.COM >wrote in message
news: XXXX@XXXXX.COM ...

>I have a pointer to mdi child form.
>let's say
>TmyMDIChild *myPointer=new TmyMDIChild(this);
>
>and then
>on OnClose event I have
>Action=caFree;
>
>but I as well need to asign to myPointer=NULL;
>at the moment when mdichild is closed...
>when can I do that?

Unless you actually use the pointer after creating the form instance, you
don't need to set it to NULL. Especially if you are using the same
pointer
to create multiple form instances. The code you showed is using the
pointer
as a local variable, so chances are it won't even be in scope anymore by
the
time the form is closed.


Gambit


 

Re:when to assign NULL to pointer

"George" < XXXX@XXXXX.COM >wrote in message
Quote
the code that I showed doesn't really show the real code!
Please always show real code.
Quote
and I know I don't need to set it to NULL..
I just need in other part of a program to check if that
form is created, and thought that would be the best way
to know it.
The code you showed isn't going to be able to do that anyway, because your
pointer is local in scope. You would need a pointer that is declared as
either a member of the main form, or declared globally, in order to do what
you are asking. Otherwise, the code will have to loop through all of the
available MDI child forms until it finds the one it is interested in.
Gambit