Note that there are some explanatory texts on larger screens.

plurals
  1. POSmart pointers and this in long running method
    text
    copied!<p>there are related questions like <a href="https://stackoverflow.com/questions/382166/smart-pointers-this-considered-harmful">smart pointers + &quot;this&quot; considered harmful?</a> but they do not handle my case. All these questions are about exposing a raw this pointer in case of reference counting smart pointers. However, my problem is not that I do expose <code>this</code> but rather use it only in the method, but maybe for a longer period of time.</p> <p>Consider the following code:</p> <pre><code>class X{ void foo(){...} //Performs some long running task } shared_ptr&lt;X&gt; x; void bar(){ x-&gt;foo(); } </code></pre> <p>Okay, so some code calls the <code>foo</code> method of object x. Consider that the only smart reference to the instance behind x is the one shared_ptr x. Now, foo performs some long running task invoking other functions and stuff.</p> <p>Now, consider, while foo is running, another thread, a signal handler, or even a recursive call in <code>foo</code> changes or clears the reference <code>x</code>. This would trigger the deletion of the object. Now, the <code>this</code> pointer on the call stack of <code>foo</code> points to a deleted object. Further code execution in <code>foo</code> will therefore produce unpredictable results.</p> <p>How to avoid this? I mean, whenever being in the execution of a method of an object that is handled via reference counting, there is the danger that some code might clear the references to the object and the method call will fail or produce strange results. The problem is the semantics: The reference counting thinks that there is no more reference to the object and thus deletes it, but this is not true - there is still the <code>this</code> reference, but sadly, it is not a smart pointer.</p> <p>I know there is stuff like <code>enable_shared_from_this</code>, but should I ALWAYS rewrite all methods of objects that could potentially be reference counted to first obtain a shared pointer from <code>this</code> and then use that pointer, to be save from this problem? This would clutter all method code, and in addition, it might still fail: If an event clears <code>x</code> after the call to foo has been made but before the first statement in the method (which obtains a shared instance) executes (maybe another thread), then things will fail again.</p> <p>So the general question is: When using any kind of smart pointers, how can I be safe during a method call to the managed object? How can I ensure that the <code>this</code> pointer inside the method will not become invalid? Is rewriting all methods to use <code>enable_shared_from_this</code> in their first statement a good solution? </p> <p>Or is code that clears a reference while that reference is still on the call stack simply ill-formed? But if it is, then it is hard to write non-ill-formed code once multiple threads and complex recursive calls are used...</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