Sign in to follow this  
mderdem

Delete and need for a idea

Recommended Posts

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

Share this post


Link to post
Share on other sites
snk_kid    1312
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 increments

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

Share this post


Link to post
Share on other sites
nmi    978
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.

Share this post


Link to post
Share on other sites
snk_kid    1312
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this