[Pyrex] callback
Bryan Weingarten
bryan.weingarten at pobox.com
Tue Aug 19 15:06:41 CEST 2003
> > class Myplay:
> > def status(self, sec, msec):
> > print sec, msec, self.data
> > return 1
> >
> > def play(self, mp3file):
> > self.data = ('test', 2.0, {1:'one'})
> > m = xxx.Play(mp3file)
> > m.status_callback(self.status)
while(m.play_frame()): pass
> >
> > if __name__ == '__main__':
> > m = Myplay()
> > m.play(sys.argv[1])
> >
>
> Your problem here is reference counting again. The
> expression 'self.status' creates a new bound method object every time it
> executes.
> m.status_callback must increment the reference count on the callback
> parameter otherwise the bound method created by 'self.status' will be
> destroyed as soon as the call the status_callback returns. The nested
> function version is probably working more by luck than anything else.
>
> So long as you only have one callback per Play object you could just save
> the callback in the Play object, that way it will be kept alive just as
> long as you need it. Something like:
>
> class Play:
> def status_callback(self, callback):
> set_status_callback(self.ctx, _callback_wrapper, <void*>callback)
> self.callback = callback
>
> --
> Duncan Booth duncan at rcp.co.uk
i've added a line to my program above.... maybe this will give you more of a
hint as to why i'm crashing... everytime m.play_frame() is called, status is
called once.
this way seems to grind my computer a bit before it crashes. status never
gets called
class Play:
def status_callback(self, callback):
set_status_callback(self.ctx, _callback_wrapper, <void*>callback)
self.callback = callback
this way crashes immediately. again, status never gets called
class Play:
def status_callback(self, callback):
Py_INCREF(callback)
set_status_callback(self.ctx, _callback_wrapper, <void*>callback)
both ways still work for a nested status function in the python code.
bryan
More information about the Pyrex
mailing list