[Bf-python] Re: Crazy tooltip bug

Stephen Swaney sswaney at swbell.net
Fri Mar 26 07:36:42 CET 2004


nicolas bonnand wrote:
> 
> I've tried your script, the problem also happens under linux.
> 
> I'm not sure, but I would say there's a problem with
> the memory management of the tooltip variable
> in the draw function

Yep.  This is exactly what is going on.

> It behaves as if the malloc of the tooltip
> variable was cleared when leaving  the draw
> function.
> (normally it should not happen, even if the tooltip
> variable is local to the draw function, because the variable
> goes on being used.... But...Maybe something is
> wrong with the reference count of the tooltip var ?
> Py_INCREF  ???? , ...)

The catch here is that inside Number() we call 
PyArg_ParseTuple and get a C pointer to an existing C string
that is part of the tooltip argument.  This C pointer is then
 passed into uiDefButF() and friends.  This gives us a C pointer
 into a Python string that, unfortunately in this case, goes out 
of scope when the draw() function exits.

It is a memory management issue, but I would not call it a 
python reference counting problem.  It is more of an issue with 
the interface between blender's C code and our python 
implementation.
 
> An ugly workaround is to give Number() a tooltip variable that is  not
> local of the draw function

It's not *that* ugly and it has the virtue of working!

The other approach would be to copy the character data out
of the python tooltip string into a C structure that would
then be managed by the blender ui code.  I didn't dive deeply
enough into the UI internals to see how the responsibilty for
memory allocation is passed around, but someone would have
to handle freeing up the struct to prevent a memory leak.

-- 
Stephen Swaney			
sswaney at swbell.net



More information about the Bf-python mailing list