Re: Crash using callback

Thomas Heller <[email protected]> Thu, 05 Oct 2006 20:10:16 +0200
Newsgroups gmane.comp.python.ctypes.devel
Message-ID <[email protected]>
Peter Hansen schrieb:
> Victor Stinner wrote:
>> Question: would it be possible to change object lifetime, tell that the 
>> function keep a referrence of the object, or something like that? Or should I 
>> keep a copy of "mycallback = callback_t(mine)" somewhere?
> 
> I'm pretty sure you must keep a reference in Python, or it will be (via 
> reference counting) freed up immediately.  I'm also (slightly less, but 
> still fairly) sure that the documentation on callbacks points something 
> like this out... maybe double-check there again?
> 
Peter is completely right.  You need to keep the callback object alive
*in Python* until it is no longer used by any C code.
If ctypes would keep it alive forever automatically, we would have a memory
leak.

Thomas


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV