I wrote the following simple code,
#include <iostream>
#include <memory>
struct Foo
{
Foo() { std::cout << "Foo::Foo\n"; }
~Foo() { std::cout << "Foo::~Foo\n"; }
void bar() { std::cout << "Foo::bar\n"; }
};
int main()
{
std::shared_ptr<Foo> p1(new Foo);
p1->bar();
std::shared_ptr<Foo> p2(p1);
}
And in the watch window I got
p2 std::__1::shared_ptr<Foo> ptr = 0x100104350 strong=2 weak=1
p1 std::__1::shared_ptr<Foo> ptr = 0x100104350 strong=2 weak=1
Now I can understand strong = 2, but why weak = 1?
Secondly, in the code, I changed the code to,
std::shared_ptr<Foo> p1(std::move(new Foo));
Because maybe one weak pointer is due to an unnamed object. So I moved it when creating p1, but I still get the same result in the viewport.
Please indicate which moment I am missing?
source
share