[shared_ptr] boost::shared_ptr deleter question

Hi All, I wonder when the shared_ptr deleter is destroyed. I have a shared_ptr with a custom deleter. I would expect when the last shared pointer to the object is gone then not only the deleter method will be called, but the deleter function object is also released. As I see the deleter is taken by value and stored by value in the reference counter, so I do not understand what is going on. How is the deleter mean to be freed? Thanks, Tamas struct MyClassDeleter { ~MyClassDeleter() { // Break point is set here } void operator()(MyClass* item) { // Break point is set here } } { auto sharedPtr = boost::shared_ptr<MyClass>(new MyClass, MyClassDeleter) } // shared_ptr leaves the scope here, the custom deleter is called (parenthesis operator) // the deleter instance is however not destroyed, the breakpoint in the destructor does not hit

Tamas, I find it very surprising that your breakpoint in the destructor is not being hit at all. This: #include <boost/smart_ptr/shared_ptr.hpp> #include <iostream> struct D { ~D() { std::cout << "~D\n"; } template<class U> void operator()(U*) { std::cout << "operator()\n"; } }; struct T { }; int main() { { auto p = boost::shared_ptr<T>(new T, D{}); std::cout << "...\n"; } } Outputs: ~D ~D ... operator() ~D Glen -- View this message in context: http://boost.2283326.n4.nabble.com/shared-ptr-boost-shared-ptr-deleter-quest... Sent from the Boost - Users mailing list archive at Nabble.com.

Hi Glen, You are right, my example was not complete. Finally I have figured the problem out: I had also a weak_ptr somewhere to the owned class and it did not let the deleter to be removed. It means: - Owned class is removed when there is no more strong refrence exist. - Deleter is destroyed when neither strong nor weak references exist The reason is: the deleter is stored by value in the reference counter class. I guess, theoretically the deleter would be not necessary any more after the owned object is destroyed, but storing it in the reference counter was maybe simpler to implement (maybe more efficient as well). Thanks and sorry for the noise. Cheers, Tamas From: Glen Fernandes <glen.fernandes@gmail.com> To: boost-users@lists.boost.org, Date: 14.08.2015 15:50 Subject: Re: [Boost-users] [shared_ptr] boost::shared_ptr deleter question Sent by: "Boost-users" <boost-users-bounces@lists.boost.org> Tamas, I find it very surprising that your breakpoint in the destructor is not being hit at all. This: #include <boost/smart_ptr/shared_ptr.hpp> #include <iostream> struct D { ~D() { std::cout << "~D\n"; } template<class U> void operator()(U*) { std::cout << "operator()\n"; } }; struct T { }; int main() { { auto p = boost::shared_ptr<T>(new T, D{}); std::cout << "...\n"; } } Outputs: ~D ~D ... operator() ~D Glen -- View this message in context: http://boost.2283326.n4.nabble.com/shared-ptr-boost-shared-ptr-deleter-quest... Sent from the Boost - Users mailing list archive at Nabble.com. _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Glen Fernandes
-
Tamas.Ruszkai@leica-geosystems.com