Talk:cpp/memory/shared ptr/reset
From cppreference.com
The statement: "If *this already owns an object and it is the last shared_ptr owning it, the object is destroyed through the owned deleter, unless ptr is a pointer to it." is misleading at best. std::shared_ptr::reset(ptr) is guaranteed by the standard to be equal to std::shared_ptr( ptr ).swap( *this ). This reference implementation does not provide any facilities to handle pointers to the stored object any different, than other pointers, so doing a std::shared_ptr::reset() on the pointer already owned will always cause a double free. See: [1]
I suggest to drop the "unless ptr is a pointer to it" part or clarify somehow what is actually meant by this. Till 06:08, 20 March 2012 (PDT)
- Yes it is unclear. This website is a wiki, you are welcome to edit it on your own since you've already looked up the information. --Cubbi 09:00, 20 March 2012 (PDT)
- Yes, I know and I will edit it now. However I usually like to confirm in more detail, what could be meant by a statement before flately deleting it. Somebody might have had some completely different idea than I did, which might make complete sense. Till 13:47, 20 March 2012 (PDT)
- Good catch, Till. That was pretty misleading. :) Nate 15:43, 20 March 2012 (PDT)
- I am the guilty editor. That statement was just wrong, there weren't any fancy ideas hiding. I'll add an explicit note about shared_ptr<T> a; ... ; a.swap(a.get()); -- P12 16:22, 20 March 2012 (PDT)