[Bf-python] CurNurb/IpoCurve/BezTriple hackery

Stephen Swaney sswaney at centurytel.net
Sat Jun 18 18:48:56 CEST 2005


On Sat, Jun 18, 2005 at 09:23:34AM -0700, Ken Hughes wrote:

> But if the underlying object is changed, you don't expect for
> Python to scribble to memory.  Iterators make it harder to do but
> not impossible:
> 
>   l=[]
>   for p in some_ipo_curve:
>      l.append(p)
> 
>   some_ipo_curve.addBezier([1,2])
>   l[0].setPoint([3,4])	# writes to freed memory
> 
> 

> 
> True.... bad example on my part.  But
> 
>   a = Blender.Ipo.Get('ObIpo')
>   b = a.getCurve('LocX')
>   a.delCurve('LocX')
>   b.setInterpolation('Constant') # accesses freed memory
> 
> is the same type of problem.
> 
> Again, I'm not proposing that these are or aren't valid things to do
> in a BPython script.  I'm just saying I think they shouldn't cause
> Blender to malfunction if they do happen.

I would argue that both these examples are bad programming practice.
Managing object lifetime is a critical part of Object Oriented 
programming.

What you are doing here is getting a reference to an object 
( a sequence in this case ) and then modifying the object
by adding or deleting elements.  At that point, the reference
you are holding is invalid.

You can do the same thing is C by allocating an array and
setting another pointer to it.  If you then realloc the
array, the held pointer is probably not valid.

This is another argument for deprecating  get/set methods
and using attribute access.

>   a = Blender.Ipo.Get('ObIpo')
>   b = a.getCurve('LocX')
>   a.delCurve('LocX')
>   b.setInterpolation('Constant') # accesses freed memory

if    b.setInterpolation('Constant')
 becomes something like a['LocX'].setIterpolation() you can throw
an exception when the 'LocX' lookup fails.
-- 
Stephen Swaney			
sswaney at centurytel.net




More information about the Bf-python mailing list