问题
Test program:
#include <tbb/parallel_invoke.h>
int main(void)
{
tbb::parallel_invoke([]{},[]{});
return 0;
}
- Compiled using
g++ -std=c++11 tmp.cpp -ltbb
Checked with
valgrind --tool=memcheck --track-origins=yes \ --leak-check=full --log-file=report ./a.out`
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?
回答1:
Most likely, it's a false positive, not a bug. There are at least few reasons:
- TBB uses its own memory allocator
libtbbmalloc
, it caches the memory till the process termination and can appear as a leak. - 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:
- remove libtbbmalloc.so.2 or tbbmalloc.dll file so running an application with env.variable
TBB_VERSION=1
will outputTBB: ALLOCATOR malloc
but notTBB: ALLOCATOR scalable_malloc
- 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;
}
来源:https://stackoverflow.com/questions/16598161/tbb-possible-memory-leak