<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Issue 2529: Assigning to enable_shared_from_this::__weak_this twice</title>
<meta property="og:title" content="Issue 2529: Assigning to enable_shared_from_this::__weak_this twice">
<meta property="og:description" content="C++ library issue. Status: Resolved">
<meta property="og:url" content="https://cplusplus.github.io/LWG/issue2529.html">
<meta property="og:type" content="website">
<meta property="og:image" content="http://cplusplus.github.io/LWG/images/cpp_logo.png">
<meta property="og:image:alt" content="C++ logo">
<style>
  p {text-align:justify}
  li {text-align:justify}
  pre code.backtick::before { content: "`" }
  pre code.backtick::after { content: "`" }
  blockquote.note
  {
    background-color:#E0E0E0;
    padding-left: 15px;
    padding-right: 15px;
    padding-top: 1px;
    padding-bottom: 1px;
  }
  ins {background-color:#A0FFA0}
  del {background-color:#FFA0A0}
  table.issues-index { border: 1px solid; border-collapse: collapse; }
  table.issues-index th { text-align: center; padding: 4px; border: 1px solid; }
  table.issues-index td { padding: 4px; border: 1px solid; }
  table.issues-index td:nth-child(1) { text-align: right; }
  table.issues-index td:nth-child(2) { text-align: left; }
  table.issues-index td:nth-child(3) { text-align: left; }
  table.issues-index td:nth-child(4) { text-align: left; }
  table.issues-index td:nth-child(5) { text-align: center; }
  table.issues-index td:nth-child(6) { text-align: center; }
  table.issues-index td:nth-child(7) { text-align: left; }
  table.issues-index td:nth-child(5) span.no-pr { color: red; }
  @media (prefers-color-scheme: dark) {
     html {
        color: #ddd;
        background-color: black;
     }
     ins {
        background-color: #225522
     }
     del {
        background-color: #662222
     }
     a {
        color: #6af
     }
     a:visited {
        color: #6af
     }
     blockquote.note
     {
        background-color: rgba(255, 255, 255, .10)
     }
  }
</style>
</head>
<body>
<hr>
<p><em>This page is a snapshot from the LWG issues list, see the <a href="lwg-active.html">Library Active Issues List</a> for more information and the meaning of <a href="lwg-active.html#Resolved">Resolved</a> status.</em></p>
<h3 id="2529"><a href="lwg-defects.html#2529">2529</a>. Assigning to <code>enable_shared_from_this::__weak_this</code> twice</h3>
<p><b>Section:</b> 20.3.2.7 <a href="https://wg21.link/util.smartptr.enab">[util.smartptr.enab]</a> <b>Status:</b> <a href="lwg-active.html#Resolved">Resolved</a>
 <b>Submitter:</b> Jonathan Wakely <b>Opened:</b> 2015-08-26 <b>Last modified:</b> 2020-09-06</p>
<p><b>Priority: </b>3
</p>
<p><b>View all other</b> <a href="lwg-index.html#util.smartptr.enab">issues</a> in [util.smartptr.enab].</p>
<p><b>View all issues with</b> <a href="lwg-status.html#Resolved">Resolved</a> status.</p>
<p><b>Discussion:</b></p>
<p>
It is unclear what should happen if a pointer to an object with an
<code>enable_shared_from_this</code> base is passed to two different <code>shared_ptr</code>
constructors.
</p>
<blockquote><pre>
#include &lt;memory&gt;

using namespace std;

int main()
{
  struct X : public enable_shared_from_this&lt;X&gt; { };
  auto xraw = new X;
  shared_ptr&lt;X&gt; xp1(xraw);  // #1
  {
    shared_ptr&lt;X&gt; xp2(xraw, [](void*) { });  // #2
  }
  xraw-&gt;shared_from_this();  // #3
}
</pre></blockquote>
<p>
This is similar to LWG <a href="lwg-defects.html#2179" title="enable_shared_from_this and construction from raw pointers (Status: Resolved)">2179</a><sup><a href="https://cplusplus.github.io/LWG/issue2179" title="Latest snapshot">(i)</a></sup>, but involves no undefined behaviour due
to the no-op deleter, and the question is not whether the second
<code>shared_ptr</code> should share ownership with the first, but which <code>shared_ptr</code>
shares ownership with the <code>enable_shared_from_this::__weak_this</code> member.
<p/>
With all three of the major <code>std::shared_ptr</code> implementations the <code>xp2</code>
constructor modifies the <code>__weak_this</code> member so the last line of the
program throws <code>bad_weak_ptr</code>, even though all the requirements on the 
<code>shared_from_this()</code> function are met (20.3.2.7 <a href="https://wg21.link/util.smartptr.enab">[util.smartptr.enab]</a>)/7:
</p>
<blockquote><p>
<i>Requires</i>: <code>enable_shared_from_this&lt;T&gt;</code> shall be an accessible base class
of <code>T</code>. <code>*this</code> shall be a subobject of an object <code>t</code> of type <code>T</code>. There shall
be at least one <code>shared_ptr</code> instance <code>p</code> that owns <code>&amp;t</code>.
</p></blockquote>
<p>
Boost doesn't update <code>__weak_this</code>, leaving it sharing with <code>xp1</code>, so the
program doesn't throw. That change was made to <code>boost::enable_shared_from_this</code> because 
someone reported exactly this issue as a bug, see <a href="https://svn.boost.org/trac/boost/ticket/2584">Boost issue 2584</a>.
<p/>
On the reflector Peter Dimov explained that there are real-world use
cases that rely on the Boost behaviour, and none which rely on the
behaviour of the current <code>std::shared_ptr</code> implementations. We should
specify the behaviour of <code>enable_shared_from_this</code> more precisely, and
resolve this issue one way or another.
</p>

<p><i>[2016-03-16, Alisdair comments]</i></p>

<p>
This issues should be closed as <code>Resolved</code> by paper <a href="https://wg21.link/p0033r1">p0033r1</a> at Jacksonville.
</p>


<p id="res-2529"><b>Proposed resolution:</b></p>





</body>
</html>
