[C++-sig] Release Python's lock before destructing pointer_holder

Roman Yakovenko roman.yakovenko at gmail.com
Sat Mar 24 18:50:36 CET 2007


On 3/23/07, Gabriel Becedillas <gabriel.becedillas at corest.com> wrote:
> I'm wrapping a class whose destructor does some work on sockets that
> might take some time to complete, so I need to release Python's lock
> before calling the destructor.
> This instances are held via boost::shared_ptr. One way I could manage to
> do so is to declare a destructor for pointer_holder<>  like this:
>
> pointer_holder::~pointer_holder()
> {
>         PyThreadState* prev = PyThreadState_Swap(NULL);
>         PyEval_ReleaseLock();
>
>         m_p = Pointer();
>
>         PyEval_AcquireLock();
>         PyThreadState_Swap(prev);
> }
>
> There are 2 reasons why I don't like this solution. The first one is
> that I have to modify boost::python's code. The second is that I need
> this only for a certain class.
> Does anyone know a better/cleaner way to do this ?
> Thanks.
>

boost::shared_ptr class takes custom deleter in constructor.
You can use it and solution provided in "How can I find the existing
PyObject that holds a C++ object?" Boost.Python FAQ to solve this
issue.

-- 
Roman Yakovenko
C++ Pytdhon language binding
http://www.language-binding.net/



More information about the Cplusplus-sig mailing list