[vtkusers] Python memory buildup

Yves Starreveld ystarrev at julian.uwo.ca
Tue Aug 29 13:41:24 EDT 2000


Hi, Randy,

I am glad someone else has run across this!

I recently have been doing some iterative updating of a deformation grid
with the same problem.

I added a bunch of debugging code to the python wrappers and there
appears to be a problem with an object's __del__ method not always being called.

Any vtk object that has a GetOutput method creates a Python object which
does not have a Python name attached to it. (it escapes the reference
counting mechanism this way).
So while the filter unregisters itself from the object when it goes out
of scope, the object still has this unreachable python object floating
around keeping it around.

Clearly I should fix this, but unfortunately, I found a workaround.

If you declare a vtkImageData object first, then use the SetOutput
method to attach this to your filter (or reader), then the reference
counting works properly.

An example:

def SubtractGrids(self, centA, centB, gridPiece):
    pieceMath = vtkImageMathematics()    
    pieceMath.SetInput1(centA)
    pieceMath.SetInput2(centB)
    mathOut = vtkImageData()
    pieceMath.SetOutput(mathOut)
    pieceMath.SetOperationToSubtract()
    pieceMath.Update()

    pieceReslice = vtkImageReslice()
    pieceReslice.SetOutput(gridPiece)
    pieceReslice.SetInterpolationModeToLinear()
    pieceReslice.SetInput(mathOut)
    pieceReslice.SetOutputExtent(gridPiece.GetWholeExtent())
    pieceReslice.Update()
    pieceReslice.UnRegisterAllOutputs()



If you call this multiple times, memory leaks like a sieve. Declaring
mathout and using SetOutput stuff fixes the problem, since the
vtkImageMathematics Output now is bound and gets its __del__ method
called when it goes out of scope.

I hope this helps.

Yves




More information about the vtkusers mailing list