Does the previous pointer automatically get destroyed (or dereferenced) in an std::shared_ptr if I assign a new one to it with the = operator?
For example:
std::shared_ptr< Type > sp1 (ptr1, std::ptr_fun(destroy));
std::shared_ptr< Type > sp2 (ptr2);
sp1 = sp2; // now, will ptr1 be dereferenced and / or destroyed?
// and will the destroy() function get called?
Yes, or else you would have a leak and it would defeat the purpose of having a smart ptr.
Just made a quick test and I didn't get any leaks
#define BOOST_TEST_MODULE leakTest
#include <boost/test/unit_test.hpp>
BOOST_AUTO_TEST_CASE( Leak_Test )
{
std::shared_ptr< int > sp1 (new int(3));
std::shared_ptr< int > sp2 (new int(4));
sp1 = sp2;
}
Results:
Running 1 test case...
* No errors detected Press any key to continue .
. .