Suppose I create a shared pointer with a custom deleter. In the following code I would like to check what happens with the deleter object itself:
struct A {
A() { std::cout << "A\n"; }
~A() { std::cout << "~A\n"; }
};
struct D {
D() {
std::cout << "D\n";
}
~D() {
std::cout << "~D\n";
}
D(const D&) {
std::cout << "D(D&)\n";
}
void operator()(A* p) const {
std::cout << "D(foo)\n";
delete p;
}
};
int main()
{
std::shared_ptr<A> p(new A, D());
}
I see that D(const D&)
and ~D()
for the "deleter" class are called six more times:
D
A
D(D&)
D(D&)
D(D&)
D(D&)
D(D&)
D(D&)
~D
~D
~D
~D
~D
~D
D(foo)
~A
~D
What happens? Why it needs to be copied so many times?
shared_ptr
at all, as it is the worst smart pointer in terms of performance.weak_ptr
is the only reason to ever useshared_ptr
. – Epigeous