• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
metsfan

Reference Counting Base Class

7 posts in this topic

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
0

Share this post


Link to post
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.

0

Share this post


Link to post
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...

2

Share this post


Link to post
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.

0

Share this post


Link to post
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
1

Share this post


Link to post
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.

0

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  
Followers 0