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 &#39;foo.h&#39;:<br>&nbsp; struct FooStruct:<br>&nbsp;&nbsp;&nbsp; pass<br>&nbsp; FooStruct* Foo_new()<br>&nbsp; int Foo_call(FooStruct* self, int indexFunc)<br>&nbsp; int Foo_addFunc(FooStruct* self, void(*func)(void*), void* data)<br>
&nbsp; int Foo_fire(FooStruct* self)<br>&nbsp; void Foo_destroy(FooStruct* self)<br><br><br>cdef void PyCallback( object funcAndData):<br>&nbsp; cdef object func, data<br>&nbsp; func = funcAndData[0]<br>&nbsp; data = funcAndData[1]<br>&nbsp; func(*data)<br>
<br>cdef class PyFooStruct:<br>&nbsp; cdef FooStruct* selfPtr<br>&nbsp; cdef object tmpFuncAndData<br><br>&nbsp; def __init__(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.selfPtr = Foo_new()<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.tmpFuncAndData = []<br><br>&nbsp; def PyFoo_call(self, i):<br>
&nbsp;&nbsp;&nbsp; return Foo_call(self.selfPtr, i)<br><br>&nbsp; def PyFoo_addFunc(self, func, data):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cdef object tmpFuncAndData<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; tmpFuncAndData = (func, data)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; self.tmpFuncAndData.append(tmpFuncAndData)<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Foo_addFunc(self.selfPtr, &lt;void(*)(void*)&gt;PyCallback, \<br>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &lt;void*&gt;tmpFuncAndData)<br><br>&nbsp; def PyFoo_fire(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return Foo_fire(self.selfPtr)<br><br>&nbsp; def __dealloc__(self):<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; Foo_destroy(self.selfPtr)<br><br><br><div class="gmail_quote">
2008/5/9 Robert Bradshaw &lt;<a href="mailto:robertwb@math.washington.edu">robertwb@math.washington.edu</a>&gt;:<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>