Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<blockquote> <p>Why shared_ptr pObj doesn't report (at run-time) that underlying object has already been deleted?</p> </blockquote> <p>Because <code>shared_ptr</code> is not <em>magic</em><sup>1</sup>. It knows when the contained object has been deleted only when <em>it deletes that object</em>. When you use a <code>shared_ptr</code>, you have entered into a contract with <code>shared_ptr</code>. One of the tenants of that contract (indeed, of <em>any</em> contract you enter into with a smart pointer of any kind) is that <em>you</em> don't get to delete the pointer. The <code>shared_ptr</code> instance owns the pointer, and <em>it</em> will delete it, not you.</p> <p>Violating that contract leads to undefined behavior.</p> <blockquote> <p>Since I'm creating a const shared_ptr, means can't use it to refer any other object, why lambda is not invoked at object deletion.</p> </blockquote> <p>Again, the <code>shared_ptr</code> can only know that the contained object is deleted when <em>it</em> deletes it. It knows nothing about the state of the object if you break the contract.</p> <blockquote> <p>Can weak_ptr be helpful in similar cases. weak_ptr is used with semantics that the lifetime of a reference to an object outlives the object it refers to.</p> </blockquote> <p><code>weak_ptr</code> is no more magically endowed than <code>shared_ptr</code>. <code>weak_ptr</code> only knows what the <code>shared_ptr</code> set it was created with knows about. If the <code>shared_ptr</code> doesn't know that the object's been deleted, the <code>weak_ptr</code> won't either.</p> <hr> <p><sup>1</sup> By "magic", I mean doing something that's just not possible in C++. If you want to know that a function has been called (and a destructor is a function call), there are only two ways to do that. Either that function tells you it's been called (by setting some value you can see), or you set up a system whereby people call your function which then calls the other function.</p> <p>The first system requires a function that is written explicitly to let people know that it's been called. You can't do that with any old function; it has to be designed for that. The second system requires that everyone use your new function and nobody uses the old one. If someone uses the old one directly, your code won't know about it.</p> <p>The first method is called "intrusive" (because it requires that you write your object in a special way), and smart pointers that use it are called "intrusive smart pointers". The second method is non-intrusive (doesn't require special code for the object). <code>shared_ptr</code>, and all currently standard smart pointers, are non-intrusive. This means you can use them with any object, but you can only use them if you abide by the contract.</p> <p>C++ does not offer a third way. Therefore, a class that could somehow intrude on a destructor call, a class that could know it has been called without that destructor explicitly <em>telling</em> it that it has been, is not possible. And therefore would be magic.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload