• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1<html>
2<head>
3<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
4<title>How can I wrap a function which needs to take ownership of a raw pointer?</title>
5<link rel="stylesheet" href="../boostbook.css" type="text/css">
6<meta name="generator" content="DocBook XSL Stylesheets V1.79.1">
7<link rel="home" href="../index.html" title="Boost.Python">
8<link rel="up" href="../faq.html" title="Chapter 5. Frequently Asked Questions (FAQs)">
9<link rel="prev" href="how_can_i_find_the_existing_pyob.html" title="How can I find the existing PyObject that holds a C++ object?">
10<link rel="next" href="compilation_takes_too_much_time_.html" title="Compilation takes too much time and eats too much memory! What can I do to make it faster?">
11</head>
12<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
13<table cellpadding="2" width="100%"><tr><td valign="top"><img alt="" width="" height="" src="../images/boost.png"></td></tr></table>
14<hr>
15<div class="spirit-nav">
16<a accesskey="p" href="how_can_i_find_the_existing_pyob.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="compilation_takes_too_much_time_.html"><img src="../images/next.png" alt="Next"></a>
17</div>
18<div class="section">
19<div class="titlepage"><div><div><h3 class="title">
20<a name="faq.how_can_i_wrap_a_function_which0"></a><a class="link" href="how_can_i_wrap_a_function_which0.html" title="How can I wrap a function which needs to take ownership of a raw pointer?">How can I wrap
21      a function which needs to take ownership of a raw pointer?</a>
22</h3></div></div></div>
23<p>
24        <span class="bold"><strong>Q:</strong></span> Part of an API that I'm wrapping goes
25        something like this:
26      </p>
27<pre class="programlisting"><span class="keyword">struct</span> <span class="identifier">A</span> <span class="special">{};</span> <span class="keyword">struct</span> <span class="identifier">B</span> <span class="special">{</span> <span class="keyword">void</span> <span class="identifier">add</span><span class="special">(</span> <span class="identifier">A</span><span class="special">*</span> <span class="special">);</span> <span class="special">}</span>
28<span class="identifier">where</span> <span class="identifier">B</span><span class="special">::</span><span class="identifier">add</span><span class="special">()</span> <span class="identifier">takes</span> <span class="identifier">ownership</span> <span class="identifier">of</span> <span class="identifier">the</span> <span class="identifier">pointer</span> <span class="identifier">passed</span> <span class="identifier">to</span> <span class="identifier">it</span><span class="special">.</span>
29</pre>
30<p>
31        However:
32      </p>
33<pre class="programlisting"><span class="identifier">a</span> <span class="special">=</span> <span class="identifier">mod</span><span class="special">.</span><span class="identifier">A</span><span class="special">()</span>
34<span class="identifier">b</span> <span class="special">=</span> <span class="identifier">mod</span><span class="special">.</span><span class="identifier">B</span><span class="special">()</span>
35<span class="identifier">b</span><span class="special">.</span><span class="identifier">add</span><span class="special">(</span> <span class="identifier">a</span> <span class="special">)</span>
36<span class="identifier">del</span> <span class="identifier">a</span>
37<span class="identifier">del</span> <span class="identifier">b</span>
38<span class="preprocessor"># python</span> <span class="identifier">interpreter</span> <span class="identifier">crashes</span>
39<span class="preprocessor"># later</span> <span class="identifier">due</span> <span class="identifier">to</span> <span class="identifier">memory</span> <span class="identifier">corruption</span><span class="special">.</span>
40</pre>
41<p>
42        Even binding the lifetime of a to b via <code class="computeroutput"><span class="identifier">with_custodian_and_ward</span></code>
43        doesn't prevent the python object a from ultimately trying to delete the
44        object it's pointing to. Is there a way to accomplish a 'transfer-of-ownership'
45        of a wrapped C++ object?
46      </p>
47<p>
48        --Bruce Lowery
49      </p>
50<p>
51        Yes: Make sure the C++ object is held by auto_ptr:
52      </p>
53<pre class="programlisting"><span class="identifier">class_</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">auto_ptr</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="special">&gt;(</span><span class="string">"A"</span><span class="special">)</span>
54  <span class="special">...</span>
55  <span class="special">;</span>
56</pre>
57<p>
58        Then make a thin wrapper function which takes an auto_ptr parameter:
59      </p>
60<pre class="programlisting"><span class="keyword">void</span> <span class="identifier">b_insert</span><span class="special">(</span><span class="identifier">B</span> <span class="special">&amp;</span><span class="identifier">b</span><span class="special">,</span> <span class="identifier">std</span><span class="special">::</span><span class="identifier">auto_ptr</span><span class="special">&lt;</span><span class="identifier">A</span><span class="special">&gt;</span> <span class="identifier">a</span><span class="special">)</span>
61<span class="special">{</span>
62  <span class="identifier">b</span><span class="special">.</span><span class="identifier">insert</span><span class="special">(</span><span class="identifier">a</span><span class="special">.</span><span class="identifier">get</span><span class="special">());</span>
63  <span class="identifier">a</span><span class="special">.</span><span class="identifier">release</span><span class="special">();</span>
64<span class="special">}</span>
65</pre>
66<p>
67        Wrap that as B.add. Note that pointers returned via <code class="computeroutput"><span class="identifier">manage_new_object</span></code>
68        will also be held by <code class="computeroutput"><span class="identifier">auto_ptr</span></code>,
69        so this transfer-of-ownership will also work correctly.
70      </p>
71</div>
72<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
73<td align="left"></td>
74<td align="right"><div class="copyright-footer">Copyright © 2002-2015 David
75      Abrahams, Stefan Seefeld<p>
76        Distributed under the Boost Software License, Version 1.0. (See accompanying
77        file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
78      </p>
79</div></td>
80</tr></table>
81<hr>
82<div class="spirit-nav">
83<a accesskey="p" href="how_can_i_find_the_existing_pyob.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../faq.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="compilation_takes_too_much_time_.html"><img src="../images/next.png" alt="Next"></a>
84</div>
85</body>
86</html>
87