Search code examples
pythonccythonwrapperlinphone

Cython weakref to cdef class stored in C object


I'm trying to write a Cython wrapper for the linphone library, but I'm facing an issue: I'd like to store a weakref of the Python object representing my C object inside the C object.

Here's my wrapper:

cdef void on_global_state_changed_cbs(LinphoneCore *lc, LinphoneGlobalState gstate, const char *message):
    print('Internal Callback : ' + str(gstate) + ' : ' + message)
    cbs = linphone_core_get_current_callbacks(lc)
    callbacks = CoreCallbacks.from_ptr(cbs)
    if callbacks.global_state_cb is not None:
        core = Core.from_ptr(lc)
        callbacks.global_state_cb(core, gstate, message)

ctypedef struct LinphoneCoreCbs:
    pass

cdef class CoreCallbacks:
    cdef object __weakref__
    cdef LinphoneCoreCbs *ptr
    cpdef global_state_cb

    @staticmethod
    cdef CoreCallbacks from_ptr(LinphoneCoreCbs *_ptr, take_ref = True):
        cdef CoreCallbacks coreCbs
        user_data = belle_sip_object_data_get(<belle_sip_object_t *>_ptr, 'python_user_data')

        if user_data is not NULL:
            tmpWRef = <object>user_data
            print tmpWRef
            coreCbs = tmpWRef()
            if coreCbs is None:
                coreCbs = CoreCallbacks.__new__(CoreCallbacks)
                coreCbs.ptr = linphone_core_cbs_ref(_ptr) if take_ref else _ptr
        else:
            coreCbs = CoreCallbacks.__new__(CoreCallbacks)
            coreCbs.ptr = linphone_core_cbs_ref(_ptr) if take_ref else _ptr

        print coreCbs
        wref = weakref.ref(coreCbs)
        print wref
        belle_sip_object_data_set(<belle_sip_object_t *>_ptr, 'python_user_data', <void *>wref, NULL)
        linphone_core_cbs_set_global_state_changed(coreCbs.ptr, &on_global_state_changed_cbs)

        return coreCbs

    def __dealloc__(self):
        print 'Dealloc ' + str(self)
        if self.ptr is not NULL:
            belle_sip_object_data_remove(<belle_sip_object_t *>self.ptr, 'python_user_data')
            linphone_core_cbs_unref(self.ptr)
        self.ptr = NULL

    @property
    def on_global_state_changed_cb(self):
        return self.global_state_cb

    @on_global_state_changed_cb.setter
    def on_global_state_changed_cb(self, cb):
        self.global_state_cb = cb

Problem is when I call Core.from_ptr from a callback, the tmpWRef says the object is dead and thus create a new one. I added a print inside the dealloc method and I see the object hasn't been dealloc'ed yet.

My test program:

def global_state(lc, state, message):
    print ('External callback : ' + str(state) + ' -> ' + message)

f = Factory.get_instance()
c = f.create_core()
cbs = f.create_core_cbs()
cbs.on_global_state_changed_cb = global_state
c.add_callbacks(cbs)
c.start()

Here's a sample output of my test program:

<pylinphone.CoreCallbacks object at 0x7f7211fe6c80>
<weakref at 0x7f7212001368; to 'pylinphone.CoreCallbacks' at 0x7f7211fe6c80>
Internal Callback : 1 : Starting up
<weakref at 0x7f7212001368; dead>
<pylinphone.CoreCallbacks object at 0x7f7212000460>
<weakref at 0x7f7212001368; to 'pylinphone.CoreCallbacks' at 0x7f7212000460>
Dealloc <pylinphone.CoreCallbacks object at 0x7f7212000460>
[...]
Dealloc <pylinphone.CoreCallbacks object at 0x7f7211fe6c80>

Solution

  • Thanks to DavidW I found the problem: the weakref wasn't held by anyone. I added a cpdef wref field to my class and now it works fine :)