[C++-sig] Fwd: [boost-python] Making a reference to the PyObject* in construct()

Austin Bingham austin.bingham at gmail.com
Fri Sep 25 15:01:18 CEST 2009


I'm trying to figure out an issue that seems to have to do with the
construct() method in a from-python conversion. I'm converting from a
python string to a custom C++ string class. The c++ string just holds
a reference to a python string using a boost::python::object. My
construct() function looks like this:

 struct String_from_python_str
 {
   . . .
   static void construct(
     PyObject* obj_ptr,
     boost::python::converter::rvalue_from_python_stage1_data* data)
     {
       using namespace boost::python;

       void* storage = (
           (converter::rvalue_from_python_storage<String>*)
            data)->storage.bytes;
         new (storage) String(object(handle<>(obj_ptr)));  // <---- Is
this correct? Should it be borrowed?
         data->convertible = storage;
     }
 };

My program has started seeing occasional python reference counting
problems and what appear to be memory corruption problems. I find that
if I use a borrowed() handle when constructing my string, the problems
go away. Is this correct? Or am I doing something totally else wrong
here?

I can't find any documentation re: the "borrowed" nature of obj_ptr,
so I really just assumed that it was a normal, pre-incremented
reference. Any help on this would be great. Thanks.

Austin Bingham


More information about the Cplusplus-sig mailing list