• 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
oliii

boost::intrusive_ptr problem

13 posts in this topic

I run into this problem...
Quote:
#include <memory.h>
#include <stdio.h>
#include <boost/smart_ptr.hpp>

#define DECLAREREF(Type) class Type; typedef boost::intrusive_ptr<Type> Type##Ref;
#define NULLREF(Type) boost::intrusive_ptr<Type>()
template<typename Type> boost::intrusive_ptr<Type> MAKEREF(Type* object) { return boost::intrusive_ptr<Type>(object); }
template<typename Type> bool ISNULLREF(const boost::intrusive_ptr<Type>& ref) { ref == boost::intrusive_ptr<Type>(); }


namespace boost
{
	class Referencable
	{
	protected:
		Referencable() : refcount_(0) {}
		long refcount_;

		virtual void incRef() { (++refcount_); }
		virtual void decRef() { if((--refcount_) == 0) delete this; }

	private:	
		friend void intrusive_ptr_release(Referencable * p);
		friend void intrusive_ptr_add_ref(Referencable * p);
	};

	void intrusive_ptr_add_ref(Referencable * p)
	{
		p->incRef();
	}

	void intrusive_ptr_release(Referencable * p)
	{
		p->decRef();
	}
}

class DummyClass
{
public:
	virtual ~DummyClass(){}
	virtual bool doSomethingVirtual()=0;
};

DECLAREREF(Object);
class Object: public DummyClass, public boost::Referencable
{
public:
	Object() {}
	virtual ~Object() {}
	virtual bool doSomethingVirtual() { return true; }
};

#ifdef WIN32
#include <windows.h>					// Include the much need windows.h
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hprev, PSTR cmdline, int ishow)
#else
void main()
#endif
{
	ObjectRef object = MAKEREF(new Object());
	object = NULLREF(Object);
}
Basically, I have a base class Referencable, that implements reference counting and release. Objects needed intrusive pointer support are derived from it. The problem comes with multiple inheritance. The Object is also derived from another class.
Quote:
class Object: public DummyClass, public boost::Referencable
{
    //....
};
this throws an exception at the pointer deletion, the Referencable pointer if offset by 4 bytes from the Object pointer. if I do this
Quote:
class Object: public boost::Referencable, public DummyClass
{
    //....
};
It's fine. I don't like it. I'm not sure if it's wrong and I am an imbecile, or if there is a better way to do this without having to worry about multiple inheritance. I'd like to derive the 'Referencable' so I can use it for a recycle bin (which I do and works fine so far).
0

Share this post


Link to post
Share on other sites
also, I tried this as well.

Quote:


namespace boost
{
class Referencable
{
protected:
Referencable() : refcount_(0) {}
long refcount_;

virtual void incRef(Referencable * p) { ++(p->refcount_); }
virtual void decRef(Referencable * p) { if(--(p->refcount_) == 0) delete p; }

private:
friend void intrusive_ptr_release(Referencable * p);
friend void intrusive_ptr_add_ref(Referencable * p);
};

void intrusive_ptr_add_ref(Referencable * p)
{
p->incRef(p);
}

void intrusive_ptr_release(Referencable * p)
{
p->decRef(p);
}
}


No luck. I'm thinking maybe some template trickery...
0

Share this post


Link to post
Share on other sites
doh! I noticed that when the destructors were not triggered in my app.

Fixed. thx.
0

Share this post


Link to post
Share on other sites
Why not? I don't mean that in a sarcastic way, I'm all ears. but I prefer them to shared pointers for my purpose. I get more control over the object's deletion.

- I want to delegate the deletion of the object to a recycle bin. To do that with shared pointers, I'll have to override the delete() operator (which is ok).
- I need to recycle a lot of objects per frame, which are pulled back from that recycle bin.
- No heap allocation required for the ref counter.
- I can use the ref counter to signal an object manager an object is no longer referenced (ref counter == 1).
0

Share this post


Link to post
Share on other sites
Quote:
Original post by oliii
- I want to delegate the deletion of the object to a recycle bin. To do that with shared pointers, I'll have to override the delete() operator (which is ok).


Actually you don't, you can just specify a deleter functor to the shared_ptr when you construct it from a pointer. This functor can do whatever you want with the object, it's just the default functor that calls delete.
1

Share this post


Link to post
Share on other sites
A couple of quick comments:
Quote:
- I want to delegate the deletion of the object to a recycle bin. To do that with shared pointers, I'll have to override the delete() operator (which is ok).
- I need to recycle a lot of objects per frame, which are pulled back from that recycle bin.
You can do pooled allocation using shared_ptr as well, e.g. by using boost::pool and implementing operator new and operator delete on a per-class basis.
Quote:
- No heap allocation required for the ref counter.
boost::shared_ptr has a (perhaps undocumented) feature, a define that turns on automated pooled allocation for ref counters (or at least that's my understanding). I'm not sure if this feature is offered by tr1::shared_ptr though.
Quote:
- I can use the ref counter to signal an object manager an object is no longer referenced (ref counter == 1).
shared_ptr has a function called unique() which, I think, could be used to accomplish the same thing.

I'm not arguing against the use of intrusive_ptr at all, but I just thought I'd mention the above points about using shared_ptr, just in case.
1

Share this post


Link to post
Share on other sites
I did have a look at pools... iirc, there were some problems, I forgot what exactly!
0

Share this post


Link to post
Share on other sites
Ah yes, the shared pointer deletion calls the destructor. That means, if the object has dynamic memory, the memory will be deleted as part of the destructor cleanup, and then needs to be re-allocated when it's pooled again. Which kinda makes it useless for me.

hmmm, again, I could be looking at it in the wrong way.
0

Share this post


Link to post
Share on other sites
Well, as I said earlier what a shared_ptr does when the lifetime of the managed object has ended can be easily configured:


void moveToPool(MyObject* obj) { ... }

typedef shared_ptr<MyObject> MyObjectPointer;

...

MyObjectPointer pObject(new MyObject(...), moveToPool);




In the above example the function moveToPool will be called when the last instance of the shared pointer expires. Note that you can copy the shared_ptr as often as you want, all instances will keep track of the deleter function.

This is also very useful if you want to manage an entity which has to be cleaned up by a library function instead of delete (for example cvReleaseImage in OpenCV).
0

Share this post


Link to post
Share on other sites
Quote:
Original post by oliii
Why not?

Because if your only reason whas this:
Quote:
Original post by oliii
- No heap allocation required for the ref counter.

Then I would have yelled make_shared ;)

It's a function in boost that heap allocates the object and the refcount with a single allocation.
0

Share this post


Link to post
Share on other sites
That make_shared looks pretty handy, as I'd been concerned about the performance implications too. Even searching the Boost mailing list implied this was still an issue. It's a shame that the Boost libraries are so sprawling and difficult to keep track of - who knows how many useful things pass us by.
0

Share this post


Link to post
Share on other sites
Quote:
Original post by BitMaster
Well, as I said earlier what a shared_ptr does when the lifetime of the managed object has ended can be easily configured:

*** Source Snippet Removed ***

In the above example the function moveToPool will be called when the last instance of the shared pointer expires. Note that you can copy the shared_ptr as often as you want, all instances will keep track of the deleter function.

This is also very useful if you want to manage an entity which has to be cleaned up by a library function instead of delete (for example cvReleaseImage in OpenCV).


That's interesting, I'll look into that. make_shared() also good :)
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