Why is the weak reference count of a std::shared_ptr not zero after std::make_shared invoked?

162 Views Asked by At

After executing lines 40 and 41 below, the debugger is telling me that the weak reference count for the std::shared_ptr's a and b is 2.

enter image description here

The ctor for Value is not storing any strong or weak references to these objects:

struct Value : public std::enable_shared_from_this<Value> {
    double data;
    double grad;
    std::function<void()> _backward;
    std::vector<std::shared_ptr<Value>> _prev;
    std::string _op;
    Value(double data,
          const std::vector<std::shared_ptr<Value>>& _children = {},
          std::string _op = "")
        : data{data}, grad{0}, _backward{[]{}}, _prev{_children}, _op{_op} {}
    ...
};

I expect there to be only single strong references to these objects (we can see the expected strong reference counts of 1 in the shared_ptr control block). In fact there are no explicit std::weak_ptr's being use anywhere in this code base? Is the debugger itself storing weak reference's to these objects (i.e., a Heisenbug issue)?

Line 42, which has yet to be executed, invokes an overloaded + operation, but I don't see how this would affect the current (or future for that matter) weak reference counts:

std::shared_ptr<Value> operator+(const std::shared_ptr<Value>& A,
                                 const std::shared_ptr<Value>& B) {
   ...
}
0

There are 0 best solutions below