Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

C++ Objects

0 views
Skip to first unread message

Lasse Skyum

unread,
Oct 17, 2003, 4:29:34 AM10/17/03
to
Hi all,

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


Paul Morriss

unread,
Oct 17, 2003, 8:42:38 AM10/17/03
to
You could always check for NULL, eg if (ptr_3dengine == NULL) { delete
ptr_3dengine; }

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...

Jan Langermann

unread,
Oct 17, 2003, 1:25:52 PM10/17/03
to
> 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.
>
For testing purposes, you may add some sort of console output to the
object's constructor.

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)

Andrew R. Gillett

unread,
Oct 17, 2003, 6:54:52 PM10/17/03
to
In alt.games.programming, Paul Morriss wrote:
> You could always check for NULL, eg if (ptr_3dengine == NULL) { delete
> ptr_3dengine; }

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/

Lasse Skyum

unread,
Oct 18, 2003, 8:46:00 AM10/18/03
to

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.

--
Lasse

Peter Ashford

unread,
Oct 19, 2003, 10:42:52 PM10/19/03
to
Lasse Skyum wrote:
> 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.
>
>

If it ends up working nicely, let us know what your system does when
you're finished.

Chris Whitworth

unread,
Oct 20, 2003, 5:30:27 AM10/20/03
to
"Lasse Skyum" <no spam> wrote in
news:3f913626$0$27378$edfa...@dread16.news.tele.dk:

>
> 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

0 new messages