Jump to content
  • Advertisement
Sign in to follow this  

Reference Counting Pointers

This topic is 3715 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

I've heard a lot about reference counting "smart pointers" as a way to prevent memory leaks, and I'm a little confused. I'm envisioning something that basically holds a regular ("dumb") pointer and an int, which is incremented when a new instance of the class is created. What I'm confused about is, how does the pointer know when either a) a new object of the class is created or b) another smart pointer goes out of scope, without the use of some global object. Can anyone explain this to me?

Share this post


Link to post
Share on other sites
Advertisement
a) you assign the new object to it.
b) it uses a common object, not a global one. The common object is passed around when you copy the smart pointer.

Share this post


Link to post
Share on other sites
It depends on the programming language. In C++ reference counted smart pointers are class objects and the reference counts are adjusted whenever the constructor, destructor or assignment operators of the smart pointer objects are invoked. In some other languages, the reference counts are managed by the language runtime.

Share this post


Link to post
Share on other sites
Well, you could have a variable within the class, lets call x, whenever you attach another reference to your pointer you could increment x by 1. Then when, your smart pointer goes out of scope,you could decrement x(in your destructor), then you'd check if x is 0. If x is 0, then nothing else is referencing to your data, and you can delete, else, someone else is referencing, and you wouldnt delete it.

Hope that helps.

Share this post


Link to post
Share on other sites
Note: I am assuming you are talking about implementing a reference counted pointer in C++.

First point: if you choose to write one yourself, once you are done throw it away and use a tried and tested version, like boost::shared_ptr

The reference-counted-pointer does not hold a pointer and an int. Instead, it holds a pointer to the object *and* a *pointer* to an integer. The integer, because it is referenced through the pointer, is shared among all the reference-counted-pointers just like the object. A simple example:

template<class Type>
class shared_ptr
{
public:
shared_ptr()
:
pointer(0),
count(0)
{
}

shared_ptr( Type *ptr )
:
pointer(ptr),
count(0)
{
if(pointer)
{
count = new int(1);
}
}

shared_ptr( const shared_ptr &other )
:
pointer(other.pointer),
count(other.count)
{
if(pointer)
{
++(*count);
}
}

// operator=() too

~shared_ptr()
{
if(pointer)
{
--(*count);
if((*count) == 0)
{
delete pointer;
delete count;
}
}
}

private:
Type *pointer;
int *count;
};



The above was neither compiled nor tested in any way could easily have an error in it, it is just to show the idea.

Share this post


Link to post
Share on other sites
Quote:
Original post by rip-off
The above was neither compiled nor tested in any way could easily have an error in it, it is just to show the idea.


And just to show how hard it is to get right: the Type * constructor in that source is not exception safe.

Share this post


Link to post
Share on other sites
Thank you all for the explainations - this is just what I wanted. Yes, I was talking about a reference pointer implementation in C++, and the idea of storing a pointer to the reference count hadn't occurred to me.

I know I should use boost:: pointers, this is really just a learning exercise. I like to understand at least one way a given concept could work before I make heavy use of it. This makes sense though - thanks.

Share this post


Link to post
Share on other sites
Of course, the source code for all the boost libraries is freely available. You can just look at how boost implements it's shared_ptr class.

Share this post


Link to post
Share on other sites
Yes, but reading and understanding are sometimes very different things, especially for those of us who are less-experienced. I like to implement my own... this is just how I learn things.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!