Reference Counting Base Class

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

Recommended Posts

Hello all,

So I have this crazy idea (at least it seems crazy to me, but I kinda like crazy  ) to have a base class that all the classes in my game engine extend that manages a reference count.  The idea is to create a reference counting system that is more efficient than std::shared_ptr but also provides the safety of knowing an object won't be deleted while it's still being referenced.  Here is the WIP code I have so far (obviously it is far from complete, but it's enough for me to start testing):

BaseObject.h:

class BaseObject
{
public:
BaseObject();

// Decrements Reference Count, Deletes when it reaches zero
void Release();

std::string GetType();

// Assign a new pointer, with automatic reference count management, and return it.
static BaseObject * Apply(BaseObject *dst, BaseObject *src);

protected:
std::string mType;

private:
volatile unsigned int mRefCount;
};

BaseObject.cpp:






BaseObject::BaseObject()
{
mRefCount = 1;
}

BaseObject::~BaseObject()
{

}

{
InterlockedIncrementAcquire(&mRefCount);
}

void BaseObject::Release()
{
InterlockedDecrementAcquire(&mRefCount);

if(mRefCount == 0) {
delete this;
}
}

BaseObject * BaseObject::Apply(BaseObject *dst, BaseObject *src)
{
if(dst != src)
{
if(dst != NULL) {
dst->Release();
}

if(src != NULL) {
dst = src;
return dst;
} else {
return NULL;
}
}

return dst;
}

So as can clearly be seen, when the object is initialized, its ref count is set to 1 (meaning you must call release when you are done with it, unless you allocated on the stack or with a scoped_ptr in which case it will be destroyed when the scope ends).  Then, when you want to assign the object to another pointer, you would call apply, such as { obj1 = obj1->Apply(obj2); }

Does this make sense?  Is there any way I can improve this?  Is this whole idea crazy?  Any tips or suggestions would be extremely helpful.  Thank you.

EDIT - well found one problem already, which is if you call this->Release(), and it deletes, then no value is ever returned, since the object is now destroyed.  Perhaps it would work better if Apply was a static method (so it would be BaseObject::Apply(BaseObject *dst, BaseObject *src), with similar logic).

EDIT2 - Updated Apply to be static.

Edited by metsfan

Share on other sites
Two thoughts: 1) If you consider this == null to be a valid use case then there's something wrong with your design. 2) There is a smart pointer you can use for objects with an embedded reference count: boost::intrusive_ptr.

On 1), yes I agree with you.  That is why I think I am going to change my Apply method to be static, so that it's ok if one of the references passed in is NULL.  As per 2), I really want to stay away from templated pointers.  I understand why they were designed that way, but I want my entire application to be reference counted (well all heap allocated poitners anyway).   I like the fact that my pointers will use the default C/C++ pointer syntax, but still be reference counted.  I know many will not agree with this, but after working a lot with shared_ptr objects, I'd rather not ever deal with templated pointers again.  I just didn't find it to be an enjoyable experience.

Share on other sites

[qoute]

I like the fact that my pointers will use the default C/C++ pointer syntax, but still be reference counted.

[/qoute]

You've already "broken" the default pointer syntax by introducing the "Apply" method, and forcing the programmer to remember to call Release.

I don't see how this is more like the default pointer syntax, then a templated reference counted smart pointer with overloaded assignment operator.

To me, it seems to be the other way around...

Share on other sites

Also,

(1) consider the use of the CRTP idiom so you can avoid the Singly Rooted Inheritance Heirarchy antipattern (feel free to use Google).  This is how what you're trying to do has been done by the experts in their published examples.

(2) How does your class handle objects created on the stack?  Objects that are members of another class?

(3) This is a procedural (C-style) interface.  You're going to be spending a long time chasing reference problems.  Consider the use of an OO design to avoid that.

Share on other sites

Thanks for the replies.  I was hoping there would be a way that I could create a fully reference counted application that didn't require template pointers, but I suppose that isn't going to be possible.  I didn't want to have to have every single pointer in my project be a template pointer, but it seems like there's no good way around that.  Thanks for the tips.

Share on other sites

Frankly, I didn't give it much thought yet, but the first feeling towards your solution is kind of "rejective".

first, you say it yourself, it feels "crazy".

secondly : Bregma point number 1.

you are going to have to explain yourself about the "I didn't find it to be an enjoyable experience".

Also, please consider that some people just are on the edge of banning any usage of shared counted pointers (like google C++ guidelines) because it lacks a strong owner with clear memory responsibilities.

I don't advocate anything yet, I am using shared_ptr for help with RAII. and also avoid return by copy but still have return-result idiom and not "pass by reference an 'out' argument" which I dislike. (I don't have C++11 with move semantic so... sometimes I use RVO, sometimes shard_ptr for that purpose)

I find intrusive pointers kind of dirty, though I perfectly understand the motive (memory fragmentation notably).

my 2cts, though I didn't answer anything, I just thrown some random concerns around for you to take/reject/think about etc.

Edited by Lightness1024

Share on other sites

Thank you everyone for your feedback and help.  After much thinking and weighing the comments here,  I have come to the conclusion that rather than focusing more on reference counting, I need to focus more on better pointer ownership conventions.  I'm trying to use reference counting to make the problem of pointer ownership go away, so i can just forget about it, but that's the opposite of what I should be doing.  I should be focusing harder on making pointer ownership better, so that I don't need to start throwing pointer references everywhere, and only use reference counted pointers when they are really needed.  Thanks again.

1. 1
2. 2
3. 3
Rutin
15
4. 4
khawk
13
5. 5
frob
12

• 9
• 9
• 11
• 11
• 23
• Forum Statistics

• Total Topics
633665
• Total Posts
3013247
×