Maybe not so game specific, but....
Is there a way to determine if a C++ object is created with the 'new'
operator or not? I only have a pointer to the object and would like to know
if it is okay to 'delete' it.
--
Lasse
Depending on the application and implementation of the object, make sure you
are careful not to delete an object that is being used by something else, a
good example of this would be deleting a user and finding a pointer is using
it, causing your entire world to collapse in front of you.
The method I use for checking pointers and objects is to use a reference
counter, each time I make a call to the object I increment a static counter,
when delete is called it only does the delete when the value becomes 0. I
have an example I can post if you are interested ?
Paul
"Lasse Skyum" <lskyum(AT)mail.dk> wrote in message
news:3f8f9a85$0$69928$edfa...@dread12.news.tele.dk...
If it has to be at run-time, check if that pointer is not NULL (but be sure
to initialize it with NULL before using new)
You mean if (ptr_3dengine != NULL) { delete ptr_3dengine; }
(Making sure to initialise such pointers to NULL)
--
Andrew Gillett http://argnet.fatal-design.com/
UK videogame release dates at:
http://www.release-dates.co.uk/
I needed to detect if an object, passed to a function, was created like:
CMyClass MyObj;
or
CMyClass *pMyObj = new CMyClass();
However I've solved it by overloading the new/delete operators for the class
and dooing some fancy pointer checking :-)
BTW, it's for some C++ garbagecollection I'm working on.
--
Lasse
If it ends up working nicely, let us know what your system does when
you're finished.
>
> Sorry folks, I wasn't beeing verry clear...
>
> I needed to detect if an object, passed to a function, was created like:
>
> CMyClass MyObj;
>
> or
>
> CMyClass *pMyObj = new CMyClass();
>
> However I've solved it by overloading the new/delete operators for the
> class and dooing some fancy pointer checking :-)
>
> BTW, it's for some C++ garbagecollection I'm working on.
FWIW, comp.lang.c++ might be a better newsgroup to try for this sort of
thing. Chances are if it's a problem worth solving, someone else will have
already done it. If it's not a problem worth solving, you will be told so
in no uncertain terms. And then there'll be a big argument about it.
Chris
--
Chris Whitworth - Programmer at Strangelite
"back when I was young, we had to travel back in time to put the tape
in so the game would load before we died."
--otama, ua2