Jump to content

  • Log In with Google      Sign In   
  • Create Account


boost::intrusive_ptr problem


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
13 replies to this topic

#1 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 23 September 2009 - 05:15 AM

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.


Sponsor:

#2 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 23 September 2009 - 05:19 AM

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.


#3 Codarki   Members   -  Reputation: 462

Like
1Likes
Like

Posted 23 September 2009 - 06:21 AM

You are missing virtual destructor in Referencable, try if that helps.

#4 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 23 September 2009 - 06:43 AM

doh! I noticed that when the destructors were not triggered in my app.

Fixed. thx.

Everything is better with Metal.


#5 DevFred   Members   -  Reputation: 836

Like
0Likes
Like

Posted 23 September 2009 - 11:29 AM

Why are you using intrusive reference counting?

#6 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 23 September 2009 - 09:52 PM

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.


#7 BitMaster   Crossbones+   -  Reputation: 3904

Like
1Likes
Like

Posted 24 September 2009 - 12:21 AM

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.

#8 scgames   Members   -  Reputation: 1977

Like
1Likes
Like

Posted 24 September 2009 - 12:26 AM

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.

#9 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 24 September 2009 - 03:21 AM

I did have a look at pools... iirc, there were some problems, I forgot what exactly!

Everything is better with Metal.


#10 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 24 September 2009 - 03:25 AM

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.


#11 BitMaster   Crossbones+   -  Reputation: 3904

Like
0Likes
Like

Posted 24 September 2009 - 08:31 PM

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

#12 DevFred   Members   -  Reputation: 836

Like
0Likes
Like

Posted 24 September 2009 - 10:34 PM

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.

#13 Kylotan   Moderators   -  Reputation: 3338

Like
0Likes
Like

Posted 24 September 2009 - 10:59 PM

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.

#14 0BZEN   Crossbones+   -  Reputation: 2013

Like
0Likes
Like

Posted 25 September 2009 - 03:05 AM

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

Everything is better with Metal.





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS