[C++-sig] call_method and reference_existing_object results in "Attempt to return dangling pointer..."

Evgeny O. Kochenyuk evgeny at digitalfun.ru
Tue Aug 26 12:28:24 CEST 2003


Hello c++-sig,

  I've got a python class method that wraps a c++ function returning
  class reference(as "reference_existing_object"). When using
  call_method on it this error message shown:

Traceback (most recent call last):
  File "test_xxx.py", line 6, in ?
    xxx.call_getValue(T())
ReferenceError: Attempt to return dangling reference to object of type: struct C


  Here's the code:
  (Surely a reference on object such as "static C c" can't be
  "dangling"?)

#test_xxx.py####################
import xxx
class T:

    def getValue(self):
            return xxx.createC()
xxx.call_getValue(T())
#####################

//xxx.cpp///////////////
#include <boost/python.hpp>

using namespace  boost::python;

struct C{
};

C& createC(){
static C c;
    return c;
}

void call_getValue(object o){
    call_method<C&>(o.ptr(), "getValue"); 
}

BOOST_PYTHON_MODULE(xxx)
{
    class_<C>("C", no_init);
    def("createC", createC, return_value_policy<reference_existing_object>());
    def("call_getValue", call_getValue);
}
///////////////
-- 
Best regards,
 Evgeny                          mailto:evgeny at digitalfun.ru






More information about the Cplusplus-sig mailing list