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

Gabriel Becedillas gabriel.becedillas at corest.com
Tue Jun 5 22:02:23 CEST 2007


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. In my particular case I don't know when or where a
boost::shared_ptr<T> will hit refcount 0 (thats the funny part of using
smart pointers).

I took a look around and found that:
1_) shared_ptr_from_python is putting the shared_ptr_deleter.
2_) shared_ptr_to_python is checking if the boost::shared_ptr<T> holds a
shared_ptr_deleter to get the PyObject inside.

Is there a way to make shared_ptr_from_python return the original
boost::shared_ptr<T> without changing the
sp_counted_impl_pd/shared_ptr_deleter ?

Thanks.



More information about the Cplusplus-sig mailing list