# Memory Leak in VTK Python

**URL:** <https://discourse.vtk.org/t/memory-leak-in-vtk-python/14614>\
**Category:** Support\
**Created:** [September 24, 2024, 3:58pm UTC](https://discourse.vtk.org/t/memory-leak-in-vtk-python/14614 "2024-09-24T15:58:26Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Ricardo\_Roop](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ricardo_roop/32/9299_2.png) [@Ricardo\_Roop](https://discourse.vtk.org/u/Ricardo_Roop)\
**Post date:** [September 24, 2024, 3:58pm UTC](https://discourse.vtk.org/t/memory-leak-in-vtk-python/14614/1 "2024-09-24T15:58:26Z")

</div>

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

```auto
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()

```

---

<div class="post-metadata">

**Author:** ![dgobbi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/dgobbi/32/18_2.png) [@dgobbi](https://discourse.vtk.org/u/dgobbi)\
**Post date:** [September 24, 2024, 6:21pm UTC](https://discourse.vtk.org/t/memory-leak-in-vtk-python/14614/2 "2024-09-24T18:21:48Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Ricardo\_Roop](https://discourse.vtk.org/user_avatar/discourse.vtk.org/ricardo_roop/32/9299_2.png) [@Ricardo\_Roop](https://discourse.vtk.org/u/Ricardo_Roop)\
**Post date:** [September 24, 2024, 8:01pm UTC](https://discourse.vtk.org/t/memory-leak-in-vtk-python/14614/3 "2024-09-24T20:01:50Z")

</div>

Ah thank you. Switching to VTK 9.2.2 solved this!

Best,

Ricardo
