[Pyrex] When C owns all references to an object
Seth Nickell
seth at gnome.org
Mon Sep 22 01:03:11 CEST 2003
Another problem I'm facing is when I'm trying to wrapper a function that
requires a callback. Standard idiom in glib interfaces is to take a
callback function and a void * pointing to "user_data". The callback
then gets called with this data. What I do is create a Python tuple
containing my user's Python callback function, and the argument they
want passed back to their callback, and then I pass this tuple into the
glib API as "user_data". Then when my standard callback handler gets
called, it unpacks the tuple and dispatches my user's Python function
with those arguments.
def some_function(self, callback_function, callback_arg):
user_data = [callback_function, callback_arg]
return dbus_some_function(self.conn, c_callback_handler,
<void*>user_data)
cdef void c_callback_handler (void *user_data):
user_data_tup = <object>user_data
function = user_data_tup[0]
args = [ user_data_tup[1] ]
function(*args)
The problem, I believe, is that the tuple user_data defined in
some_function gets garbage collected because Python doesn't know that a
C api still has a reference to it. Is there a way I can keep this from
happening without creating some hack list that I just append every
user_data tuple to just to keep a reference floating around?
-Seth
More information about the Pyrex
mailing list