Boost C++ Libraries

...one of the most highly regarded and expertly designed C++ library projects in the world. Herb Sutter and Andrei Alexandrescu, C++ Coding Standards

This is the documentation for an old version of Boost. Click here to view this page for the latest version.
PrevUpHomeNext

I'm getting the "attempt to return dangling reference" error. What am I doing wrong?

That exception is protecting you from causing a nasty crash. It usually happens in response to some code like this:

period const &get_floating_frequency() const
{
  return boost::python::call_method<period const &>(
    m_self,"get_floating_frequency");
}

And you get:

ReferenceError: Attempt to return dangling reference to object of type:
class period

In this case, the Python method invoked by call_method constructs a new Python object. You're trying to return a reference to a C++ object (an instance of class period) contained within and owned by that Python object. Because the called method handed back a brand new object, the only reference to it is held for the duration of get_floating_frequency() above. When the function returns, the Python object will be destroyed, destroying the instance of class period, and leaving the returned reference dangling. That's already undefined behavior, and if you try to do anything with that reference you're likely to cause a crash. Boost.Python detects this situation at runtime and helpfully throws an exception instead of letting you do that.


PrevUpHomeNext