i_m_getting_the_attempt_to_retur.html 5.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <html>
  2. <head>
  3. <meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
  4. <title>I'm getting the "attempt to return dangling reference" error. What am I doing wrong?</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&#160;5.&#160;Frequently Asked Questions (FAQs)">
  9. <link rel="prev" href="../faq.html" title="Chapter&#160;5.&#160;Frequently Asked Questions (FAQs)">
  10. <link rel="next" href="is_return_internal_reference_eff.html" title="Is return_internal_reference efficient?">
  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="../faq.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="is_return_internal_reference_eff.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.i_m_getting_the_attempt_to_retur"></a><a class="link" href="i_m_getting_the_attempt_to_retur.html" title="I'm getting the &quot;attempt to return dangling reference&quot; error. What am I doing wrong?">I'm getting the
  21. "attempt to return dangling reference" error. What am I doing wrong?</a>
  22. </h3></div></div></div>
  23. <p>
  24. That exception is protecting you from causing a nasty crash. It usually happens
  25. in response to some code like this:
  26. </p>
  27. <pre class="programlisting"><span class="identifier">period</span> <span class="keyword">const</span> <span class="special">&amp;</span><span class="identifier">get_floating_frequency</span><span class="special">()</span> <span class="keyword">const</span>
  28. <span class="special">{</span>
  29. <span class="keyword">return</span> <span class="identifier">boost</span><span class="special">::</span><span class="identifier">python</span><span class="special">::</span><span class="identifier">call_method</span><span class="special">&lt;</span><span class="identifier">period</span> <span class="keyword">const</span> <span class="special">&amp;&gt;(</span>
  30. <span class="identifier">m_self</span><span class="special">,</span><span class="string">"get_floating_frequency"</span><span class="special">);</span>
  31. <span class="special">}</span>
  32. </pre>
  33. <p>
  34. And you get:
  35. </p>
  36. <pre class="programlisting"><span class="identifier">ReferenceError</span><span class="special">:</span> <span class="identifier">Attempt</span> <span class="identifier">to</span> <span class="keyword">return</span> <span class="identifier">dangling</span> <span class="identifier">reference</span> <span class="identifier">to</span> <span class="identifier">object</span> <span class="identifier">of</span> <span class="identifier">type</span><span class="special">:</span>
  37. <span class="keyword">class</span> <span class="identifier">period</span>
  38. </pre>
  39. <p>
  40. In this case, the Python method invoked by <code class="computeroutput"><span class="identifier">call_method</span></code>
  41. constructs a new Python object. You're trying to return a reference to a
  42. C++ object (an instance of <code class="computeroutput"><span class="keyword">class</span> <span class="identifier">period</span></code>) contained within and owned by that
  43. Python object. Because the called method handed back a brand new object,
  44. the only reference to it is held for the duration of <code class="computeroutput"><span class="identifier">get_floating_frequency</span><span class="special">()</span></code> above. When the function returns, the Python
  45. object will be destroyed, destroying the instance of <code class="computeroutput"><span class="keyword">class</span>
  46. <span class="identifier">period</span></code>, and leaving the returned
  47. reference dangling. That's already undefined behavior, and if you try to
  48. do anything with that reference you're likely to cause a crash. Boost.Python
  49. detects this situation at runtime and helpfully throws an exception instead
  50. of letting you do that.
  51. </p>
  52. </div>
  53. <table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
  54. <td align="left"></td>
  55. <td align="right"><div class="copyright-footer">Copyright &#169; 2002-2015 David
  56. Abrahams, Stefan Seefeld<p>
  57. Distributed under the Boost Software License, Version 1.0. (See accompanying
  58. 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>)
  59. </p>
  60. </div></td>
  61. </tr></table>
  62. <hr>
  63. <div class="spirit-nav">
  64. <a accesskey="p" href="../faq.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="is_return_internal_reference_eff.html"><img src="../images/next.png" alt="Next"></a>
  65. </div>
  66. </body>
  67. </html>