Sign in to follow this  

Reference Counting

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello all. I've been thinking about memory management recently. It's definatly NOT my strong point. I'm really a complete newb when it comes to it. The best I can do at the moment is make sure I have a 'delete' for every 'new'. I know what reference counting is and why you would use it. But I don't know HOW to use it in a way that it does benefit me in the long term. As far as I understand it you keep a count of how many things are using a particular instance of a class or something. Every time a class or function wants to use that particular instance the reference count is increased. When it's finished it calls that instances 'Remove' or 'Delete' method which will decrease the reference counter. If this method is called and the reference counter is 0 then the instance is 'Delete'ed. This obviously stops the object from being removed before anything else has finished with it and stops access violation errors and stuff. Can anyone tell me how I would use this effectively. Examples of where I might want to use it e.g. Resource Management? And any hints as to how to go about implementing it? Thank you in advance!

Share this post


Link to post
Share on other sites
Hello,

the technique that you are talking about is called "smart pointers" (a good example comes with the Boost library if you're interested in something solid and workable).

Gamedev does infact have a part of an article on this topic.

Share this post


Link to post
Share on other sites
Allocate object with new, pass the resulting pointer to a boost::shared_ptr, and then pass the latter around: it will act as a normal pointer, but the pointed object will only be deleted when all pointers disappear.

Share this post


Link to post
Share on other sites
Don't reinvent the wheel. While good the Enginuity articles are old. Take ToohrVyk's advice and use boost::shared_ptr. It's heavily tested by other developers, easy to use and all you need, if I remember right, is the header file to use it.

Share this post


Link to post
Share on other sites

This topic is 3858 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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