[C++-sig] ownership of C++ object extended in Python

Roman Yakovenko roman.yakovenko at gmail.com
Thu Mar 22 19:35:05 CET 2007


On 3/22/07, Mark Williams <mark at image-engine.com> wrote:
> Mark Williams wrote:
> > Roman Yakovenko wrote:
> >> On 3/20/07, Mark Williams <mark at image-engine.com> wrote:
> >>>>     virtual ~event_t_wrapper(){
> >>>>         if (this->m_pyobj) {
> >>>>             //Py_DECREF(this->m_pyobj);
> >>>>             this->m_pyobj = 0;
> >>>>         }
> >>>>     }
> >>>>
> >>> Many thanks for the solution - it appears to be working just fine.
> >>> However I'm wondering why the Py_DECREF line above is commented out?
> >> I don't remember. I think if you uncomment it the object will be deleted twice.
> >>
> >
> > OK. Unfortunately I'm finding that this solution does not work in the
> > general case. If the Python derived class contains any member data then
> > the program eventually segfaults - which is understandable because the
> > instance has already been destructed at the time the virtual member is
> > being called. I believe it may be the case that in many simple cases the
> > solution posted works because the memory deallocated during destructing
> > the class has not been overwritten.
> >
>
> I'm still trying to find a suitable workaround to this problem. One
> thing I've tried is modifying initialize_wrapper() in wrapper_base.hpp
> to increment the reference of the object being held. However, I have a
> feeling that it then becomes impossible to actually decrement the
> reference again, leading to a memory leak.
>
> I find it hard to believe that it's impossible to safely transfer
> ownership from Python to C++, but I don't find any clues in either the
> boost.python code or docs.

I am also looking for solution. I think I have something, but I am not sure.
I think if you create small and complete test case, submit it may be more
knowledgeable developers will be able to help you.
If I will make some progress I will let you know

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



More information about the Cplusplus-sig mailing list