Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TBB possible memory leak

Test program:

#include <tbb/parallel_invoke.h>

int main(void)
{
    tbb::parallel_invoke([]{},[]{});
    return 0;
}
  1. Compiled using g++ -std=c++11 tmp.cpp -ltbb
  2. Checked with

    valgrind --tool=memcheck --track-origins=yes \
             --leak-check=full --log-file=report ./a.out`
    
  3. libtbb version: 4.0, valgrind version: 3.8.1.

Part of the above test result:

possibly lost: 1,980 bytes in 6 blocks

Question is:

Is this a TBB bug?

Or is this possible lost actually safe, it's just some codes that valgrind does not consider safe?

like image 373
gongzhitaao Avatar asked Nov 03 '22 21:11

gongzhitaao


1 Answers

Most likely, it's a false positive, not a bug. There are at least few reasons:

  1. TBB uses its own memory allocator libtbbmalloc, it caches the memory till the process termination and can appear as a leak.
  2. TBB threads run and terminate asynchronously. It is likely that after main() termination, the worker threads are still running. It leads to the same impression for the valgrind

In order to reasonably accuse TBB for a leak, exclude the above factors, e.g:

  1. remove libtbbmalloc.so.2 or tbbmalloc.dll file so running an application with env.variable TBB_VERSION=1 will output TBB: ALLOCATOR malloc but not TBB: ALLOCATOR scalable_malloc
  2. make sure all the TBB threads are terminated

For example

int main()
{
    assert(tbb::tbb_allocator<int>::allocator_type() != tbb::tbb_allocator<int>::scalable);
    { // TBB scope
        tbb::task_scheduler_init scope;
        tbb::parallel_invoke([]{},[]{});
    } // TBB threads start termination here
    sleep(10); // wait for threads to terminate
    return 0;
}
like image 114
Anton Avatar answered Nov 12 '22 18:11

Anton