microsoft / mimalloc

mimalloc is a compact general purpose allocator with excellent performance.
MIT License
10.58k stars 865 forks source link

Memory leaks while using thread_local variables #944

Open mwichtowski opened 1 month ago

mwichtowski commented 1 month ago

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.

daanx commented 1 month ago

Yikes -- that is unexpected. Thanks for the report --I'm looking into it.

daanx commented 3 weeks ago

I cannot repro on macOS so it must be a Windows specific issue. I am currently traveling but I will get to it.

mwichtowski commented 3 weeks ago

Yes, on Linux it is ok also. Problem is Windows specific.