List Info

Thread: Re: ownership of C++ object extended in Python




Re: ownership of C++ object extended in Python
country flaguser name
Canada
2007-03-20 13:12:14
Roman Yakovenko wrote:
> On 3/20/07, Mark Williams <markimage-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.

Currently the only workaround I see is to add a
"transferOwnership" 
member function which does the same reference inc/dec as the
posted 
example, but must be explicitly called from within Python.
Unless, of 
course, there is a way of accessing the PyObject* from
within the 
wrappers constructor or somehow receiving notification when

boost::detail::initialize_wrapper() is called. The latter
would be 
preferable but I do not (yet) see whether or not it is
feasible.

Thanks,

Mark
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

Re: ownership of C++ object extended in Python
country flaguser name
Canada
2007-03-22 13:11:25
Mark Williams wrote:
> Roman Yakovenko wrote:
>> On 3/20/07, Mark Williams <markimage-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.


Mark
_______________________________________________
C++-sig mailing list
C++-sigpython.org
http:
//mail.python.org/mailman/listinfo/c++-sig

[1-2]

about | contact  Other archives ( Real Estate discussion Medical topics )