mimalloc icon indicating copy to clipboard operation
mimalloc copied to clipboard

Memory leaks while using thread_local variables

Open mwichtowski opened this issue 4 months ago • 1 comments

Here is sample program (mimalloc 1.8.7 and 2.1.7):

#include <chrono>
#include <cstdio>
#include <thread>

// Build with: cl /MD mimalloc_tl_test.cpp mimalloc-override.lib /link /INCLUDE:mi_version

class MTest
{
    char *data;
public:
    MTest() { data = (char*)malloc(1024); }
    ~MTest() { free(data); };
};

thread_local MTest tlVariable;

void threadFun( int i )
{
    printf( "Thread %d\n", i );
    std::this_thread::sleep_for( std::chrono::milliseconds(100) );
}

int main( int argc, char* argv[] )
{
    for( int i=1; ; ++i )
    {
        std::thread t( threadFun, i );
        t.join();
    }
    return 0;
}

Committed memory increases indefinitely. Without 'mimalloc', everything is ok.

mwichtowski avatar Oct 07 '24 12:10 mwichtowski