I also solve the problem putting func, data references couples into a list. Since the list is a PyFooStruct attribute, func/data references will never be decremented until the PyFooStruct is used. Could be a better solution than the first solution suggested?<br>
<br>cdef extern from 'foo.h':<br> struct FooStruct:<br> pass<br> FooStruct* Foo_new()<br> int Foo_call(FooStruct* self, int indexFunc)<br> int Foo_addFunc(FooStruct* self, void(*func)(void*), void* data)<br>
int Foo_fire(FooStruct* self)<br> void Foo_destroy(FooStruct* self)<br><br><br>cdef void PyCallback( object funcAndData):<br> cdef object func, data<br> func = funcAndData[0]<br> data = funcAndData[1]<br> func(*data)<br>
<br>cdef class PyFooStruct:<br> cdef FooStruct* selfPtr<br> cdef object tmpFuncAndData<br><br> def __init__(self):<br> self.selfPtr = Foo_new()<br> self.tmpFuncAndData = []<br><br> def PyFoo_call(self, i):<br>
return Foo_call(self.selfPtr, i)<br><br> def PyFoo_addFunc(self, func, data):<br> cdef object tmpFuncAndData<br> tmpFuncAndData = (func, data)<br> self.tmpFuncAndData.append(tmpFuncAndData)<br> return Foo_addFunc(self.selfPtr, <void(*)(void*)>PyCallback, \<br>
<void*>tmpFuncAndData)<br><br> def PyFoo_fire(self):<br> return Foo_fire(self.selfPtr)<br><br> def __dealloc__(self):<br> Foo_destroy(self.selfPtr)<br><br><br><div class="gmail_quote">
2008/5/9 Robert Bradshaw <<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On May 9, 2008, at 2:42 AM, Daniele Pianu wrote:<br>
<br>
[...]<div class="Ih2E3d"><br>
<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Is there a way to solve the problem without importing anything from Python.h?<br>
</blockquote>
<br></div>
No, unless you want to cache of the func/data manually somewhere else. Also note that this may cause a memory leak, as the reference count will never be decremented.<br><font color="#888888">
<br>
- Robert<br>
<br>
</font></blockquote></div><br>