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