boost::intrusive_ptr problem

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

This topic is closed to new replies.

Advertisement