Jump to content

  • Log In with Google      Sign In   
  • Create Account


Reference Counting Base Class


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 metsfan   Members   -  Reputation: 652

Like
0Likes
Like

Posted 17 January 2013 - 08:01 AM

Hello all, 

 

So I have this crazy idea (at least it seems crazy to me, but I kinda like crazy smile.png ) 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();

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

		// A human-readable type
		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()
{

}

void BaseObject::AddRef()
{
	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;
			dst->AddRef();
			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, 17 January 2013 - 08:30 AM.


Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9386

Like
5Likes
Like

Posted 17 January 2013 - 08:12 AM

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.

#3 metsfan   Members   -  Reputation: 652

Like
0Likes
Like

Posted 17 January 2013 - 08:23 AM

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.



#4 Olof Hedman   Crossbones+   -  Reputation: 2641

Like
2Likes
Like

Posted 17 January 2013 - 08:43 AM

[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...



#5 Bregma   Crossbones+   -  Reputation: 4747

Like
3Likes
Like

Posted 17 January 2013 - 08:59 AM

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.


Stephen M. Webb
Professional Free Software Developer

#6 metsfan   Members   -  Reputation: 652

Like
0Likes
Like

Posted 17 January 2013 - 09:49 AM

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.



#7 Lightness1024   Members   -  Reputation: 688

Like
1Likes
Like

Posted 17 January 2013 - 09:56 AM

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, 17 January 2013 - 09:57 AM.


#8 metsfan   Members   -  Reputation: 652

Like
0Likes
Like

Posted 17 January 2013 - 12:07 PM

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.






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS