For the std::unique_ptr the deleter is part of the type and the std::shared_ptr keeps it as part of its control block. Therefore in a Base-Derived-Class szenario, with a non virtual destructor in the base class, the std::unique_ptr will just delete the base class part and the std::shared_ptr also the derived class part.
#include <iostream>
#include <memory>
struct Base
{
~Base()
{
std::cout << __FUNCTION__ << std::endl;
}
};
struct Derived : public Base
{
~Derived()
{
std::cout << __FUNCTION__ << std::endl;
}
};
int main()
{
{
std::cout << "unique_ptr" << std::endl;
std::unique_ptr<Base> uPtr(new Derived);
}
std::cout << std::endl;
{
std::cout << "shared_ptr" << std::endl;
std::shared_ptr<Base> sPtr(new Derived);
}
return 0;
}
$ g++ main.cpp -std=c++11 $ ./a.out unique_ptr ~Base shared_ptr ~Derived ~Base
Never the less please always make the destructor in the base class virtual.