really quick pointer question[solved]
im creating homing missiles, and they have targets, so they hold a pointer to their target. however, if their target is later deleted, this is obviously going to cause a problem. is there a programmatic way to determine if a pointer is invalid? ( a pointer != NULL obviously wont work here ).
i really want to avoid making the target conscious that it is targeted and then tell the other missiles that its gone... thats a pain in the ass. because if the missile hits something else... or its targeted by multiple missiles... i just dont want to have to deal with that pointer crap. so, anyway i can do this?
[Edited by - xSKOTTIEx on August 2, 2005 10:23:32 PM]
You can try using a smart pointer implementation like boost::shared_ptr that has weak references, in this case boost::weak_ptr.
Smart pointers are going to be the way to go here. You need a way to increase reference counts to memory locations when something points to it...so that while the object may be dead, the pointer is still valid, and the class is not deconstructed until the last pointer is gone, which means you can check if the object you are pointing to is alive or dead by using a boolean method or something like that.
well guys i solved my problem. it may be lazy, but it will never have a bug.
basically, my game objects hold a state which tells my object manager lists whether or not they are ready to be deleted. it goes like this
1) query objects.. ready to be deleted? delete them.
2) update the remaning objects
and this is done for all of my object lists. for simplicity, ive already seperated bullets from enemies, targets, etc.
so what i did in the homing missile class update method was to use this same query for deletion (isActive()), and it it is going to be deleted ( returns false ) it detatches from the target, by setting its own pointer to null.
as long as the bullets are updated before their respective target object lists, they will always catch the deletion before it happens. that way they can just ignore their target from then on out.
because the loop is linear and not threaded, the bullets will never be updated after the targets.
i know its lazy, i know its kinda hackish - but it gets the job done without any potiential bugs (maybe?) and without introducing smart pointers or object listeners (Which is what i was trying to avoid) thanks for your help anyway! rating ++.
basically, my game objects hold a state which tells my object manager lists whether or not they are ready to be deleted. it goes like this
1) query objects.. ready to be deleted? delete them.
2) update the remaning objects
and this is done for all of my object lists. for simplicity, ive already seperated bullets from enemies, targets, etc.
so what i did in the homing missile class update method was to use this same query for deletion (isActive()), and it it is going to be deleted ( returns false ) it detatches from the target, by setting its own pointer to null.
as long as the bullets are updated before their respective target object lists, they will always catch the deletion before it happens. that way they can just ignore their target from then on out.
because the loop is linear and not threaded, the bullets will never be updated after the targets.
i know its lazy, i know its kinda hackish - but it gets the job done without any potiential bugs (maybe?) and without introducing smart pointers or object listeners (Which is what i was trying to avoid) thanks for your help anyway! rating ++.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement