Cython回调适用于函数,但不适用于绑定方法

tke*_*win 8 python callback cython

我正在实现一个基于cython的C++库接口.我实现了一个与普通函数一起工作的回调系统,但在传入绑定方法时却奇怪地失败了.这是我的cython代码的一部分:

cdef extern from "VolumeHapticTool.h":
    cdef cppclass HDButtonEvent:
        bool isPressed()
        unsigned int getButtonId()
        Vec3[float] getPosition()

    ctypedef void (*HDButtonCallback)(HDButtonEvent, void *)

cdef extern from "Scene.h":
    cdef cppclass Scene:
        Scene()
        void setDrillButtonCallback( HDButtonCallback, void*)

cdef void pyHDButtonCallback(HDButtonEvent e, void *user_data):
    print <object>user_data
    (<object>user_data)( (e.isPressed(), e.getButtonId(), topy_vec3f(e.getPosition())) )

cdef class pyScene:
    cdef Scene * m_scene
    def __init__(self):
        self.m_scene = new Scene()

    def __del__(self):
        del self.m_scene

    def setDrillButtonCallback(self, func):
        print func
        self.m_scene.setDrillButtonCallback(pyHDButtonCallback, <void*>func)
Run Code Online (Sandbox Code Playgroud)

以下是我试图称之为:

class RenderCanvas(GLCanvas):
    def __init__(self, parent):
        self.scene = cybraincase.pyScene()
        self.scene.setDrillButtonCallback(self.OnDrillButtonPress)

    def OnDrillButtonPress(self, event):
        print event 
Run Code Online (Sandbox Code Playgroud)

最初运行此代码时,将打印: <bound method RenderCanvas.OnDrillButtonPress of <UI.RenderCanvas; proxy of <Swig Object of type 'wxGLCanvas *' at 0x42b70a8> >>

这似乎是正确的.问题是当触发回调时,会打印出来: <bound method Shell.readline of <wx.py.shell.Shell; proxy of <Swig Object of type 'wxStyledTextCtrl *' at 0x3a12348> >>

正在调用一种完全不同的绑定方法.但是,当我创建OnDrillButtonPress静态方法时, <function OnDrillButtonPress at 0x042FC570> 会在初始化和触发调用时打印.

是否与保存绑定方法不兼容为void*?

tke*_*win 3

感谢 Stefan Behnel 和 Mark Florisson 在 cython 讨论组上的评论,问题是对我的绑定方法的引用超出了范围并被垃圾收集。

解决方案是这样做:

cdef class pyScene:
    cdef Scene * m_scene
    cdef object drill_button_func

    def setDrillButtonCallback(self, func):
        self.m_scene.setDrillButtonCallback(pyHDButtonCallback, <void*>func)
        self.drill_button_func = func
Run Code Online (Sandbox Code Playgroud)

通过在类中保留对绑定方法的引用,直到停止使用它才会被清理。