This file is indexed.

/usr/share/doc/pybind11-doc/html/advanced/smart_ptrs.html is in pybind11-doc 2.0.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
  <meta charset="utf-8">
  
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  
  <title>Smart pointers &mdash; pybind11 2.0.1 documentation</title>
  

  
  

  

  
  
    

  

  
  
    <link rel="stylesheet" href="../_static/css/theme.css" type="text/css" />
  

  
    <link rel="stylesheet" href="../_static/theme_overrides.css" type="text/css" />
  

  
        <link rel="index" title="Index"
              href="../genindex.html"/>
        <link rel="search" title="Search" href="../search.html"/>
    <link rel="top" title="pybind11 2.0.1 documentation" href="../index.html"/>
        <link rel="next" title="Type conversions" href="cast/index.html"/>
        <link rel="prev" title="Exceptions" href="exceptions.html"/> 

  
  <script src="../_static/js/modernizr.min.js"></script>

</head>

<body class="wy-body-for-nav" role="document">

  <div class="wy-grid-for-nav">

    
    <nav data-toggle="wy-nav-shift" class="wy-nav-side">
      <div class="wy-side-scroll">
        <div class="wy-side-nav-search">
          

          
            <a href="../index.html" class="icon icon-home"> pybind11
          

          
          </a>

          
            
            
              <div class="version">
                2.0
              </div>
            
          

          
<div role="search">
  <form id="rtd-search-form" class="wy-form" action="../search.html" method="get">
    <input type="text" name="q" placeholder="Search docs" />
    <input type="hidden" name="check_keywords" value="yes" />
    <input type="hidden" name="area" value="default" />
  </form>
</div>

          
        </div>

        <div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
          
            
            
                <ul>
<li class="toctree-l1"><a class="reference internal" href="../intro.html">About this project</a></li>
<li class="toctree-l1"><a class="reference internal" href="../changelog.html">Changelog</a></li>
</ul>
<p class="caption"><span class="caption-text">The Basics</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../basics.html">First steps</a></li>
<li class="toctree-l1"><a class="reference internal" href="../classes.html">Object-oriented code</a></li>
<li class="toctree-l1"><a class="reference internal" href="../compiling.html">Build systems</a></li>
</ul>
<p class="caption"><span class="caption-text">Advanced Topics</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="functions.html">Functions</a></li>
<li class="toctree-l1"><a class="reference internal" href="classes.html">Classes</a></li>
<li class="toctree-l1"><a class="reference internal" href="exceptions.html">Exceptions</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Smart pointers</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#std-unique-ptr">std::unique_ptr</a></li>
<li class="toctree-l2"><a class="reference internal" href="#std-shared-ptr">std::shared_ptr</a></li>
<li class="toctree-l2"><a class="reference internal" href="#custom-smart-pointers">Custom smart pointers</a></li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="cast/index.html">Type conversions</a></li>
<li class="toctree-l1"><a class="reference internal" href="pycpp/index.html">Python C++ interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="misc.html">Miscellaneous</a></li>
</ul>
<p class="caption"><span class="caption-text">Extra Information</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="../faq.html">Frequently asked questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="../benchmark.html">Benchmark</a></li>
<li class="toctree-l1"><a class="reference internal" href="../limitations.html">Limitations</a></li>
<li class="toctree-l1"><a class="reference internal" href="../reference.html">Reference</a></li>
</ul>

            
          
        </div>
      </div>
    </nav>

    <section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">

      
      <nav class="wy-nav-top" role="navigation" aria-label="top navigation">
        <i data-toggle="wy-nav-top" class="fa fa-bars"></i>
        <a href="../index.html">pybind11</a>
      </nav>


      
      <div class="wy-nav-content">
        <div class="rst-content">
          

 



<div role="navigation" aria-label="breadcrumbs navigation">
  <ul class="wy-breadcrumbs">
    <li><a href="../index.html">Docs</a> &raquo;</li>
      
    <li>Smart pointers</li>
      <li class="wy-breadcrumbs-aside">
        
          
            <a href="../_sources/advanced/smart_ptrs.txt" rel="nofollow"> View page source</a>
          
        
      </li>
  </ul>
  <hr/>
</div>
          <div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
           <div itemprop="articleBody">
            
  <div class="section" id="smart-pointers">
<h1>Smart pointers<a class="headerlink" href="#smart-pointers" title="Permalink to this headline"></a></h1>
<div class="section" id="std-unique-ptr">
<h2>std::unique_ptr<a class="headerlink" href="#std-unique-ptr" title="Permalink to this headline"></a></h2>
<p>Given a class <code class="docutils literal"><span class="pre">Example</span></code> with Python bindings, it&#8217;s possible to return
instances wrapped in C++11 unique pointers, like so</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Example</span><span class="o">&gt;</span> <span class="n">create_example</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Example</span><span class="o">&gt;</span><span class="p">(</span><span class="k">new</span> <span class="n">Example</span><span class="p">());</span> <span class="p">}</span>
</pre></div>
</div>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">m</span><span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;create_example&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">create_example</span><span class="p">);</span>
</pre></div>
</div>
<p>In other words, there is nothing special that needs to be done. While returning
unique pointers in this way is allowed, it is <em>illegal</em> to use them as function
arguments. For instance, the following function signature cannot be processed
by pybind11.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="kt">void</span> <span class="nf">do_something_with_example</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">unique_ptr</span><span class="o">&lt;</span><span class="n">Example</span><span class="o">&gt;</span> <span class="n">ex</span><span class="p">)</span> <span class="p">{</span> <span class="p">...</span> <span class="p">}</span>
</pre></div>
</div>
<p>The above signature would imply that Python needs to give up ownership of an
object that is passed to this function, which is generally not possible (for
instance, the object might be referenced elsewhere).</p>
</div>
<div class="section" id="std-shared-ptr">
<h2>std::shared_ptr<a class="headerlink" href="#std-shared-ptr" title="Permalink to this headline"></a></h2>
<p>The binding generator for classes, <code class="xref cpp cpp-class docutils literal"><span class="pre">class_</span></code>, can be passed a template
type that denotes a special <em>holder</em> type that is used to manage references to
the object.  If no such holder type template argument is given, the default for
a type named <code class="docutils literal"><span class="pre">Type</span></code> is <code class="docutils literal"><span class="pre">std::unique_ptr&lt;Type&gt;</span></code>, which means that the object
is deallocated when Python&#8217;s reference count goes to zero.</p>
<p>It is possible to switch to other types of reference counting wrappers or smart
pointers, which is useful in codebases that rely on them. For instance, the
following snippet causes <code class="docutils literal"><span class="pre">std::shared_ptr</span></code> to be used instead.</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o">&lt;</span><span class="n">Example</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Example</span><span class="o">&gt;</span> <span class="cm">/* &lt;- holder type */</span><span class="o">&gt;</span> <span class="n">obj</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">&quot;Example&quot;</span><span class="p">);</span>
</pre></div>
</div>
<p>Note that any particular class can only be associated with a single holder type.</p>
<p>One potential stumbling block when using holder types is that they need to be
applied consistently. Can you guess what&#8217;s broken about the following binding
code?</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Child</span> <span class="p">{</span> <span class="p">};</span>

<span class="k">class</span> <span class="nc">Parent</span> <span class="p">{</span>
<span class="k">public</span><span class="o">:</span>
   <span class="n">Parent</span><span class="p">()</span> <span class="o">:</span> <span class="n">child</span><span class="p">(</span><span class="n">std</span><span class="o">::</span><span class="n">make_shared</span><span class="o">&lt;</span><span class="n">Child</span><span class="o">&gt;</span><span class="p">())</span> <span class="p">{</span> <span class="p">}</span>
   <span class="n">Child</span> <span class="o">*</span><span class="n">get_child</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">child</span><span class="p">.</span><span class="n">get</span><span class="p">();</span> <span class="p">}</span>  <span class="cm">/* Hint: ** DON&#39;T DO THIS ** */</span>
<span class="k">private</span><span class="o">:</span>
    <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Child</span><span class="o">&gt;</span> <span class="n">child</span><span class="p">;</span>
<span class="p">};</span>

<span class="n">PYBIND11_PLUGIN</span><span class="p">(</span><span class="n">example</span><span class="p">)</span> <span class="p">{</span>
    <span class="n">py</span><span class="o">::</span><span class="n">module</span> <span class="n">m</span><span class="p">(</span><span class="s">&quot;example&quot;</span><span class="p">);</span>

    <span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o">&lt;</span><span class="n">Child</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Child</span><span class="o">&gt;&gt;</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">&quot;Child&quot;</span><span class="p">);</span>

    <span class="n">py</span><span class="o">::</span><span class="n">class_</span><span class="o">&lt;</span><span class="n">Parent</span><span class="p">,</span> <span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Parent</span><span class="o">&gt;&gt;</span><span class="p">(</span><span class="n">m</span><span class="p">,</span> <span class="s">&quot;Parent&quot;</span><span class="p">)</span>
       <span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="n">py</span><span class="o">::</span><span class="n">init</span><span class="o">&lt;&gt;</span><span class="p">())</span>
       <span class="p">.</span><span class="n">def</span><span class="p">(</span><span class="s">&quot;get_child&quot;</span><span class="p">,</span> <span class="o">&amp;</span><span class="n">Parent</span><span class="o">::</span><span class="n">get_child</span><span class="p">);</span>

    <span class="k">return</span> <span class="n">m</span><span class="p">.</span><span class="n">ptr</span><span class="p">();</span>
<span class="p">}</span>
</pre></div>
</div>
<p>The following Python code will cause undefined behavior (and likely a
segmentation fault).</p>
<div class="highlight-python"><div class="highlight"><pre><span></span><span class="kn">from</span> <span class="nn">example</span> <span class="kn">import</span> <span class="n">Parent</span>
<span class="k">print</span><span class="p">(</span><span class="n">Parent</span><span class="p">()</span><span class="o">.</span><span class="n">get_child</span><span class="p">())</span>
</pre></div>
</div>
<p>The problem is that <code class="docutils literal"><span class="pre">Parent::get_child()</span></code> returns a pointer to an instance of
<code class="docutils literal"><span class="pre">Child</span></code>, but the fact that this instance is already managed by
<code class="docutils literal"><span class="pre">std::shared_ptr&lt;...&gt;</span></code> is lost when passing raw pointers. In this case,
pybind11 will create a second independent <code class="docutils literal"><span class="pre">std::shared_ptr&lt;...&gt;</span></code> that also
claims ownership of the pointer. In the end, the object will be freed <strong>twice</strong>
since these shared pointers have no way of knowing about each other.</p>
<p>There are two ways to resolve this issue:</p>
<ol class="arabic simple">
<li>For types that are managed by a smart pointer class, never use raw pointers
in function arguments or return values. In other words: always consistently
wrap pointers into their designated holder types (such as
<code class="docutils literal"><span class="pre">std::shared_ptr&lt;...&gt;</span></code>). In this case, the signature of <code class="docutils literal"><span class="pre">get_child()</span></code>
should be modified as follows:</li>
</ol>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">std</span><span class="o">::</span><span class="n">shared_ptr</span><span class="o">&lt;</span><span class="n">Child</span><span class="o">&gt;</span> <span class="n">get_child</span><span class="p">()</span> <span class="p">{</span> <span class="k">return</span> <span class="n">child</span><span class="p">;</span> <span class="p">}</span>
</pre></div>
</div>
<ol class="arabic simple" start="2">
<li>Adjust the definition of <code class="docutils literal"><span class="pre">Child</span></code> by specifying
<code class="docutils literal"><span class="pre">std::enable_shared_from_this&lt;T&gt;</span></code> (see <a class="reference external" href="http://en.cppreference.com/w/cpp/memory/enable_shared_from_this">cppreference</a> for details) as a
base class. This adds a small bit of information to <code class="docutils literal"><span class="pre">Child</span></code> that allows
pybind11 to realize that there is already an existing
<code class="docutils literal"><span class="pre">std::shared_ptr&lt;...&gt;</span></code> and communicate with it. In this case, the
declaration of <code class="docutils literal"><span class="pre">Child</span></code> should look as follows:</li>
</ol>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="k">class</span> <span class="nc">Child</span> <span class="o">:</span> <span class="k">public</span> <span class="n">std</span><span class="o">::</span><span class="n">enable_shared_from_this</span><span class="o">&lt;</span><span class="n">Child</span><span class="o">&gt;</span> <span class="p">{</span> <span class="p">};</span>
</pre></div>
</div>
</div>
<div class="section" id="custom-smart-pointers">
<span id="id1"></span><h2>Custom smart pointers<a class="headerlink" href="#custom-smart-pointers" title="Permalink to this headline"></a></h2>
<p>pybind11 supports <code class="docutils literal"><span class="pre">std::unique_ptr</span></code> and <code class="docutils literal"><span class="pre">std::shared_ptr</span></code> right out of the
box. For any other custom smart pointer, transparent conversions can be enabled
using a macro invocation similar to the following. It must be declared at the
top namespace level before any binding code:</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">PYBIND11_DECLARE_HOLDER_TYPE</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">SmartPtr</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">);</span>
</pre></div>
</div>
<p>The first argument of <code class="xref cpp cpp-func docutils literal"><span class="pre">PYBIND11_DECLARE_HOLDER_TYPE()</span></code> should be a
placeholder name that is used as a template parameter of the second argument.
Thus, feel free to use any identifier, but use it consistently on both sides;
also, don&#8217;t use the name of a type that already exists in your codebase.</p>
<p>The macro also accepts a third optional boolean parameter that is set to false
by default. Specify</p>
<div class="highlight-cpp"><div class="highlight"><pre><span></span><span class="n">PYBIND11_DECLARE_HOLDER_TYPE</span><span class="p">(</span><span class="n">T</span><span class="p">,</span> <span class="n">SmartPtr</span><span class="o">&lt;</span><span class="n">T</span><span class="o">&gt;</span><span class="p">,</span> <span class="nb">true</span><span class="p">);</span>
</pre></div>
</div>
<p>if <code class="docutils literal"><span class="pre">SmartPtr&lt;T&gt;</span></code> can always be initialized from a <code class="docutils literal"><span class="pre">T*</span></code> pointer without the
risk of inconsistencies (such as multiple independent <code class="docutils literal"><span class="pre">SmartPtr</span></code> instances
believing that they are the sole owner of the <code class="docutils literal"><span class="pre">T*</span></code> pointer). A common
situation where <code class="docutils literal"><span class="pre">true</span></code> should be passed is when the <code class="docutils literal"><span class="pre">T</span></code> instances use
<em>intrusive</em> reference counting.</p>
<p>Please take a look at the <a class="reference internal" href="misc.html#macro-notes"><span class="std std-ref">General notes regarding convenience macros</span></a> before using this feature.</p>
<div class="admonition seealso">
<p class="first admonition-title">See also</p>
<p class="last">The file <code class="file docutils literal"><span class="pre">tests/test_smart_ptr.cpp</span></code> contains a complete example
that demonstrates how to work with custom reference-counting holder types
in more detail.</p>
</div>
</div>
</div>


           </div>
          </div>
          <footer>
  
    <div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
      
        <a href="cast/index.html" class="btn btn-neutral float-right" title="Type conversions" accesskey="n">Next <span class="fa fa-arrow-circle-right"></span></a>
      
      
        <a href="exceptions.html" class="btn btn-neutral" title="Exceptions" accesskey="p"><span class="fa fa-arrow-circle-left"></span> Previous</a>
      
    </div>
  

  <hr/>

  <div role="contentinfo">
    <p>
        &copy; Copyright 2017, Wenzel Jakob.

    </p>
  </div>
  Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/snide/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>. 

</footer>

        </div>
      </div>

    </section>

  </div>
  


  

    <script type="text/javascript">
        var DOCUMENTATION_OPTIONS = {
            URL_ROOT:'../',
            VERSION:'2.0.1',
            COLLAPSE_INDEX:false,
            FILE_SUFFIX:'.html',
            HAS_SOURCE:  true
        };
    </script>
      <script type="text/javascript" src="../_static/jquery.js"></script>
      <script type="text/javascript" src="../_static/underscore.js"></script>
      <script type="text/javascript" src="../_static/doctools.js"></script>

  

  
  
    <script type="text/javascript" src="../_static/js/theme.js"></script>
  

  
  
  <script type="text/javascript">
      jQuery(function () {
          SphinxRtdTheme.StickyNav.enable();
      });
  </script>
   

</body>
</html>