Skip to main content
gamedev.net
🔒 Locked

Detecting invalid pointers...

Started by Jerec Jan 6, 2003 at 2:37 PM 3 replies 200+ views
Original Post
Jerec
Jerec
Ok... I have a system for attaching particle systems to objects in my game... basically all objects in the game are derived from a GameObject class that stores a position (among other things), and then I have particle systems which can be ''attached'' to game objects via a pointer to the game object (and can there by access the objects position anytime it wants)... the particle system then uses the location of the game object when spawning particles and whatnot... this allows for smoke trails and all kinds of good stuff, no matter where the object moves... it works pretty well... except when it comes to deleting objects that die... the pointer to the game object in the particle system becomes invalid and crashes because it points to unallocated memory immediatly after the object is deleted... is there anyway to detect if a pointer is bad? or what would be a better way of handling this simply? I have a few ideas, like giving game objects unique IDs which can be used to retrieve a pointer to the object if it exists... I''d like to know what other people think...
JerecCM Software"Oro?"
Monder
Monder
I don''t belive there is any way to detect if your pointer is bad. What you could do is add a function like

virtual void ParticleEmitterAttach(CParticleEmitter* Emitter);

into your gameobject class. The game object then calls Emitter->Move( ) when it moves and stuff like that. This would solve the problem
ex4st
ex4st
Maybe, you should detach your particle system when you delete an object ...

[edited by - brunow on January 6, 2003 5:20:46 PM]
paulc
paulc
Or you could try smart pointers. Try throwing it into google, should come up with a good link quickish. The idea behind them is to create a template class that wraps a pointer and provides all the functionallity of a normal pointer, but allows you to do safety/sanity checks before actually trying to access the data that the pointer points to. They also have the added bonus that they can be coded so that when the smart pointer goes out of scope it cleans up the pointer it wraps, deleting it cleanly.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.