boost::intrusive_ptr problem

Started by
12 comments, last by oliii 14 years, 7 months ago
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).

Everything is better with Metal.

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

Everything is better with Metal.

You are missing virtual destructor in Referencable, try if that helps.
doh! I noticed that when the destructors were not triggered in my app.

Fixed. thx.

Everything is better with Metal.

Why are you using intrusive reference counting?
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).

Everything is better with Metal.

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.
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.
I did have a look at pools... iirc, there were some problems, I forgot what exactly!

Everything is better with Metal.

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.

Everything is better with Metal.

This topic is closed to new replies.

Advertisement