Memory Leak in VTK Python

Hi,

I made a simple python script that slices a geometry and extracts the region closest to a point. Looping this function gives me memory leaks after the first iteration and I cannot figure out why. Any help would be greatly appreciated! Here is the function:

cylinder = surface of geometry, origin = point where to extract, tangent = plane normal, section = initialized output vtk object

def ExtractCylinderSection(cylinder, origin, normal, section):
    origin = tuple(origin)
    normal = tuple(normal)

    plane = vtk.vtkPlane()
    plane.SetOrigin(origin)
    plane.SetNormal(normal)
   
    cutter = vtk.vtkCutter()
    cutter.SetInputData(cylinder)
    cutter.SetCutFunction(plane)
    cutter.GenerateCutScalarsOn()
    cutter.SetValue(0,0.0)
    cutter.Update()
    del plane
    gc.collect()

    cleaner = vtk.vtkCleanPolyData()
    cleaner.SetInputConnection(cutter.GetOutputPort())
    cleaner.Update()
    del cutter
    gc.collect()

    if cleaner.GetOutput().GetNumberOfPoints() == 0:
        del plane
        del cutter
        del cleaner
        gc.collect()
        return 

    connectivityFilter = vtk.vtkPolyDataConnectivityFilter()
    connectivityFilter.SetInputConnection(cleaner.GetOutputPort())
    connectivityFilter.SetExtractionModeToClosestPointRegion()
    connectivityFilter.SetClosestPoint(origin)
    connectivityFilter.Update()
    section.DeepCopy(connectivityFilter.GetOutput())

    del cleaner
    del connectivityFilter
    gc.collect()
    
    if section.GetNumberOfPoints() == 0:
        return 
    section.BuildCells()
    section.BuildLinks()

The vtkCutter class had memory leaks in VTK 9.3.0 and 9.3.1 that have been fixed in the master branch.

Also note that after gc.collect() you can call vtkGarbageCollector.Collect() to run VTK’s internal garbage collector.

Ah thank you. Switching to VTK 9.2.2 solved this!

Best,

Ricardo