Delete and need for a idea
I need a design idea about the way I manage the relationships between my game objects.
GameObject is a simple game entity and it has a position and other properties.
Reader is a simple class which keeps a list of GameObject pointers. I use it like :
GameObject* obj = new GameObject ;
Reader reader ;
reader.register( obj ); // Note it takes a pointer
register method is sth like :
void register( GameObject* obj )
{
m_objectsVector.push_back( obj );
}
The problem is that the object can be deleted at some point in the game and then the reader has a pointer to non existing object then.
I would like to prevent this case. I am looking for a way to :
1. Notify the reader class if obj is being deleted
or
2. Be able to check if a pointer is a dangling pointer and the data which it points to is not valid anymore.
Do you have a nice idea which allows to implement a robust and clean approach for this problem ?
Thanks in advance,
mderdem
Quote:Original post by mderdem
Do you have a nice idea which allows to implement a robust and clean approach for this problem ?
The problem you have is known as object ownership, one solution is to use reference counted smart pointers instead of raw pointers (smart pointers are smart because they know when & how to delete memory for you at the correct time among other "smart" things), boost has several smart pointer implementations you can look here and specificially look at boost::shared_ptr its the main one you'll need.
essentially your code will look like something like this:
typedef boost::shared_ptr<GameObject> game_obj_ptr;std::vector<game_obj_ptr> vec_of_obj;/* ... */game_obj_ptr g_ptr(new GameObject);vec_of_obj.push_back(g_ptr); //reference count incrementsvec_of_obj.erase(vec_of_obj.begin()); //reference count decrements/*g_ptr still refers to GameObject here,when the last shared_ptr gets destroyed then the GameObject it refers to gets deleted for you.*/
[Edited by - snk_kid on October 18, 2004 4:09:12 AM]
Or you could just keep it simple and make a delete function in the Reader class that would delete it, instead of calling delete [] obj; directly.
The only problem about reference counting is if you have circular data structures. This may happen if your GameObject may point to another game object.
A solution to this might be to use a garbage collector.
A solution to this might be to use a garbage collector.
I'd start by using the boost.smart_ptr library. Normally you would use shared_ptr. However, there is a weak_ptr which will keep track of a shared_ptr. If the shared_ptr is deleted, the weak_ptr will get to know about it and won't be useable.
Quote:Original post by overflowed_
Or you could just keep it simple and make a delete function in the Reader class that would delete it, instead of calling delete [] obj; directly.
Doesn't prevent anyone from calling delete some where else.
Quote:Original post by nmi
The only problem about reference counting is if you have circular data structures. This may happen if your GameObject may point to another game object.
A solution to this might be to use a garbage collector.
As peterwood pointed out, you can use weak_ptr to break circular dependencies
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement