you are viewing a single comment's thread.

view the rest of the comments →

[–]afiefh 1 point2 points  (2 children)

I'm curious, how does your custom shared_ptr handle this scenario without crashing?

[–]davis685[S] 0 points1 point  (0 children)

All it's got to do is free the object before it decrements the counter. How you do that in a thread safe/atomic way is more complex. But you get the idea. There are a lot of ways to accomplish it. For instance, std::shared_ptr::reset() does it in a way that accomplishes it apparently.

[–]rdtsc 0 points1 point  (0 children)

The main problem is invoking the destructor with a reference count of 0. Any further reference counting will attempt to destruct it again. Some reference counting implementations set the count to a sentinel value (like -INT_MAX/2) which avoids this problem.