# Memory leak occurs if I use vtkSmartPointer in sub-thread.

**URL:** <https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631>\
**Category:** Development\
**Created:** [February 7, 2023, 9:19am UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631 "2023-02-07T09:19:21Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sohnishi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sohnishi/32/6507_2.png) [@sohnishi](https://discourse.vtk.org/u/sohnishi)\
**Post date:** [February 7, 2023, 9:19am UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631/1 "2023-02-07T09:19:21Z")

</div>

I’m trying to create vtkPolyData from implicit functions.  
But this code makes memory leak if I use std::thread.  
Does anyone know the workarounds?  
I’m using Linux debian11 and vtk 9.0.1 on x86\_64 PC.

```auto
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h> // for getpid()
#include <vtkContourFilter.h>
#include <vtkSmartPointer.h>
#include <vtkSampleFunction.h>
#include <vtkSphere.h>
#include <vtkPolyData.h>

long getProcessMemKB()
{
    static const long sz = getpagesize(); // bytes per a page
    std::string statmPath = "/proc/" + std::to_string(getpid()) + "/statm";
    std::ifstream ifs(statmPath.c_str());
    if(ifs.fail()) {
        return -1;
    } else {
        std::string buff;
        ifs >> buff; // virtual memory (pages)
        ifs >> buff; // physical memory (pages)
        return static_cast<long>(std::stol(buff)*sz/1024);
    }
}

std::pair<int, vtkSmartPointer<vtkPolyData>> create(int count)
{
    auto sphereSource = vtkSmartPointer<vtkSphere>::New();
    sphereSource->SetRadius(5.0);

    auto sample = vtkSmartPointer<vtkSampleFunction>::New();
    sample->SetImplicitFunction(sphereSource);
    sample->SetModelBounds(-10, 10, -10, 10, -10, 10);
    sample->SetSampleDimensions(100, 100, 100);
    sample->Update();

    auto surface = vtkSmartPointer<vtkContourFilter>::New();
    surface->SetInputConnection(sample->GetOutputPort());
    surface->Update();
    auto polyData = vtkSmartPointer<vtkPolyData>::New();
    polyData->DeepCopy(surface->GetOutput());
    return std::make_pair(count, polyData);
}

int main()
{
    int count = 0;
    while(true) {
        std::pair<int, vtkSmartPointer<vtkPolyData>> retval;

        // Memory leak occurs.
        std::thread t([&](){retval = create(++count);});
        t.join();

        // If executing in main thread, it's OK.
        // retval = create(++count);

        std::cout << "count = " << retval.first << ", Memory(MB) = " << getProcessMemKB()/1000 << std::endl;
        // doing something below in the actual code.
    }
    return 0;
}

```

---

<div class="post-metadata">

**Author:** ![sohnishi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sohnishi/32/6507_2.png) [@sohnishi](https://discourse.vtk.org/u/sohnishi)\
**Post date:** [February 8, 2023, 11:00am UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631/2 "2023-02-08T11:00:46Z")

</div>

This problem is sollved.  
I recompiled vtk libraries with changing the “VTK\_SMP\_IMPLEMENTION\_TYPE” from OpenMP to sequential.  
The memory leak stopped.

---

<div class="post-metadata">

**Author:** ![mwestphal](https://discourse.vtk.org/user_avatar/discourse.vtk.org/mwestphal/32/19_2.png) [@mwestphal](https://discourse.vtk.org/u/mwestphal)\
**Post date:** [February 8, 2023, 11:10am UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631/3 "2023-02-08T11:10:11Z")

</div>

FYI @Charles_Gueunet

---

<div class="post-metadata">

**Author:** ![Charles\_Gueunet](https://discourse.vtk.org/user_avatar/discourse.vtk.org/charles_gueunet/32/239_2.png) [@Charles\_Gueunet](https://discourse.vtk.org/u/Charles_Gueunet)\
**Post date:** [February 8, 2023, 1:31pm UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631/4 "2023-02-08T13:31:55Z")

</div>

Can you give further details on how the memory leak was detected ? Did you try the `VTK_DEBUG_LEAKS` macro or it is only the `getProcessMemKB` result?

---

<div class="post-metadata">

**Author:** ![sohnishi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sohnishi/32/6507_2.png) [@sohnishi](https://discourse.vtk.org/u/sohnishi)\
**Post date:** [February 9, 2023, 4:44am UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631/6 "2023-02-09T04:44:37Z")

</div>

I found it just by the memory usage.  
I created a littel more compact code including vtkDebugLeaks.

```auto
#include <iostream>
#include <string>
#include <thread>
#include <unistd.h> // for getpid()
#include <vtkContourFilter.h>
#include <vtkSmartPointer.h>
#include <vtkSampleFunction.h>
#include <vtkSphere.h>
#include <vtkDebugLeaks.h>

long getProcessMem()
{
    static const long sz = getpagesize(); // bytes per a page
    std::string statmPath = "/proc/" + std::to_string(getpid()) + "/statm";
    std::ifstream ifs(statmPath.c_str());
    if(ifs.fail()) return -1;
    std::string buff;
    ifs >> buff; ifs >> buff; // virtual, then physical memory (pages)
    return static_cast<long>(std::stol(buff)*sz);
}

void create()
{
    auto sphere = vtkSmartPointer<vtkSphere>::New();
    sphere->SetRadius(5.0);
    auto sample = vtkSmartPointer<vtkSampleFunction>::New();
    sample->SetImplicitFunction(sphere);
    auto surface = vtkSmartPointer<vtkContourFilter>::New();
    surface->SetInputConnection(sample->GetOutputPort());
    surface->Update(); // If comment out this line, memory usage dosen't increase.
}

int main()
{
    int count = 0;
    while(++count < 1000) {
        std::thread t1([](){create();}); // sub-thread execution
        std::thread t2([](){create();});
        t2.join();
        t1.join();
        //create(); // In the main thread. It's OK.
        std::cout << "count = " << count << ", Memory(kB) = " << getProcessMem()/1024 << std::endl;
    }
    return 0;
}

```

The output (VTK\_SMP\_IMPLEMENTION\_TYPE = OpenMP) is

```auto
count = 1, Memory(kB) = 29492
count = 2, Memory(kB) = 34204
...
count = 999, Memory(kB) = 581352
vtkDebugLeaks has detected LEAKS!
Class "vtkObjectFactoryCollection" has 1 instance still around.

```

If executing create() in the main thread, the output is

```auto
count = 1, Memory(kB) = 29688
count = 2, Memory(kB) = 32356
...
count = 999, Memory(kB) = 32356

```

---

<div class="post-metadata">

**Author:** ![sohnishi](https://discourse.vtk.org/user_avatar/discourse.vtk.org/sohnishi/32/6507_2.png) [@sohnishi](https://discourse.vtk.org/u/sohnishi)\
**Post date:** [February 16, 2023, 4:57am UTC](https://discourse.vtk.org/t/memory-leak-occurs-if-i-use-vtksmartpointer-in-sub-thread/10631/7 "2023-02-16T04:57:11Z")

</div>

Finally, I found there is no solution.

In any setting (VTK\_SMP\_IMPLEMENTION\_TYPE = OpenMP, sequential, TBB, or stdthread), the error message `Class "vtkObjectFactoryCollection" has 1 instance still around. `, which indicates a memory leak, will appear.

“VTK\_SMP\_IMPLEMENTION\_TYPE = sequential” can not perfectly prevent memory leaks, but it can reduce the amount of leakage.
