[C++-sig] Re: more problems with python::return_internal_reference<>

David Abrahams dave at boost-consulting.com
Mon Jul 14 23:52:22 CEST 2003


Stefan Seefeld <stefan.seefeld at orthosoft.ca> writes:

> hi there,
>
> I'm compiling the following code with gcc 3.2:
>
> #include <boost/python.hpp>
> namespace python = boost::python;
>
> namespace
> {
>
> class Bar {};
>
> Bar *BarFactory()
> {
>    static Bar bar;
>    return &bar;
> }
>
> }
>
> BOOST_PYTHON_MODULE(Sandbox)
> {
>     python::class_<Foo, boost::noncopyable> foo("Foo", python::no_init);
>
>     python::class_<Bar, boost::noncopyable> bar("Bar", python::no_init);
>     bar.def("foo", &Bar::foo, python::return_internal_reference<>());
>
>     python::def("Bar", BarFactory,
>                 python::return_internal_reference<>());
>                 //python::return_value_policy<python::manage_new_object>());
> }
>
> That compiles fine, but when I call the 'Bar' constructor from python,
> I get
>
>  >>> from Sandbox import *
>  >>> bar = Bar()
> Traceback (most recent call last):
>    File "<stdin>", line 1, in ?
> IndexError: tuple index out of range
>
> What is going on here ? 

return_internal_reference tries to bind the lifetime of the function's
first argument to that of the result.  Where's the first argument to
Bar()?

> What does the boost::python runtime do with the
> returned value that involves a tuple ?

Indexes into the argument tuple to do the lifetime binding.

> When I replace the 'return_internal_reference<>' policy by
> 'return_value_policy<manage_new_object>' the function call returns
> successfully. But of course, that would crash at the end of the
> application...

See C:\boost\libs\python\doc\v2\reference_existing_object.html

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com





More information about the Cplusplus-sig mailing list