3

Is it safe to self-assign a std::shared_ptr? So here is an example:

std::shared_ptr<std::vector<std::string>> pVec = std::make_shared<std::vector<std::string>>();

std::cout << pVec.use_count() << std::endl; // 1
pVec = pVec;

I know that assigning a shared_ptr object:

  • will decrement the left hand side (LHS) operand reference count (RC), and then will check whether it is 0 (these previous operations are done atomically) and if so release the resource;
  • also, will increment the right hand side (RHS) RC.

So in this example the object is the same on both LHS and RHS and the ordering of these two RC changes inside the assignment operator is unspecified.

I don't really know what happens exactly in case of self assignment.

curiousguy
  • 8,038
  • 2
  • 40
  • 58
Itachi Uchiwa
  • 3,044
  • 12
  • 26
  • Don't forget the language tag! – curiousguy Dec 13 '19 at 23:58
  • *"the order of evaluation for the assignment operator is unspecified."* - What order of evaluation? `pVec = pVec;` calls the [assignment operator](https://en.cppreference.com/w/cpp/memory/shared_ptr/operator%3D), there is no need for any extra evaluation here – UnholySheep Dec 14 '19 at 00:03
  • 1
    @UnholySheep The OP meant that the order of ownership related events (create new owner, drop ownership) is not specified. – curiousguy Dec 14 '19 at 00:24
  • 1
    @UnholySheep I changed the wording of the question to make the issue clearer, w/o taking position on the issue. I'm not validating the assertion that the order is in fact unspecified. – curiousguy Dec 14 '19 at 00:31

2 Answers2

3

Per the cppreference docs on shared_ptr's operator= (emphasis added):

Replaces the managed object with the one managed by r.

If *this already owns an object and it is the last shared_ptr owning it, and r is not the same as *this, the object is destroyed through the owned deleter.

Basically, they already thought of this possibility, and the implementation is required to handle this case; self-assignment does not delete the object, even if it's the only owner of the object.

Community
  • 1
  • 1
ShadowRanger
  • 143,180
  • 12
  • 188
  • 271
0

The self assignment is allowed and it is safe speaking about memory leak. In fact no documentation and test with valgrind show that self assignment of shared pointers to themself produce memory leak.

Zig Razor
  • 3,381
  • 2
  • 15
  • 35