# boost::intrusive_ptr problem

This topic is 3037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I run into this problem...
Quote:
 #include #include #include #define DECLAREREF(Type) class Type; typedef boost::intrusive_ptr Type##Ref; #define NULLREF(Type) boost::intrusive_ptr() template boost::intrusive_ptr MAKEREF(Type* object) { return boost::intrusive_ptr(object); } template bool ISNULLREF(const boost::intrusive_ptr& ref) { ref == boost::intrusive_ptr(); } 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 // 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).

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

##### Share on other sites
You are missing virtual destructor in Referencable, try if that helps.

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

Fixed. thx.

##### Share on other sites
Why are you using intrusive reference counting?

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

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

##### Share on other sites
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.

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

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

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

##### Share on other sites
Quote:
 Original post by oliiiWhy 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.

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

##### Share on other sites
Quote:
 Original post by BitMasterWell, 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 :)