[C++-sig] boost::shared_ptr getting back from Python with a PyObject inside

Gabriel Becedillas gabriel.becedillas at corest.com
Mon Jun 11 15:55:27 CEST 2007


Thanks Roman, but it has nothing to do with threads. Its as simple as I 
describe:

1_) Holding the GIL, you wrap a boost::shared_ptr<T> and pass it to python.
2_) Later, a C++ wrapped function gets called with that 
boost::shared_ptr<T> as an argument (since it is called from Python, 
you're holding the GIL). That function stores the boost::shared_ptr<T> 
somewhere (for example a global variable).
3_) You release the GIL.
4_) The boost::shared_ptr<T> that was passed to python, and went back 
from python, hits refcount 0, and you're not holding the GIL.

If you take a look at shared_ptr_from_python::construct you'll see a NEW 
boost::shared_ptr<T> being constructed with the original T* PLUS a 
shared_ptr_deleter(handle<>(borrowed(source))).
I don't know why this was implemented that way, instead of simply 
copying the original boost::shared_ptr<T>. I assume it was done that way 
to optimize shared_ptr_to_python, because its checking if the 
boost::shared_ptr<T> holds a shared_ptr_deleter (with a PyObject inside).
I think this was a bad choice. If I pass something to Python, and then I 
get it back from Python, I would like to get EXACTLY the same.

Roman Yakovenko wrote:
> On 6/5/07, Gabriel Becedillas <gabriel.becedillas at corest.com> wrote:
>> I have the following problem:
>> 1_) A boost::shared_ptr<T> is converted to python.
>> 2_) The PyObject holding the boost::shared_ptr<T> is converted back from
>> python, (apparently) yielding the same boost::shared_ptr<T>.
>> 3_) The boost::shared_ptr<T> hits refcount 0 somewhere else in C++Land
>> without holding the Python GIL.
>> 4_) Python's API gets called without holding the GIL because a
>> sp_counted_impl_pd with a shared_ptr_deleter modifies the PyObject's
>> refcount and Py_Dealloc gets called (check shared_ptr_deleter::operator()).
>>
>> This is wrong because you shouldn't use Python's API without holding the
>> GIL.
> 
> Did you read this FAQ:
> http://boost.org/libs/python/doc/v2/faq.html#threadsupport?
> 




More information about the Cplusplus-sig mailing list