So I’m having a strange problem that I’m hoping someone can shed some light on… I have the following code:
#include <unistd.h>
#include <mcheck.h>
#include <pthread.h>
static void *run(void *args)
{
sleep(1);
return NULL;
}
int main()
{
mtrace();
pthread_t thread;
pthread_create(&thread, NULL, run, NULL);
pthread_join(thread, NULL);
return 0;
}
And I compile I’ve compiled it in these 2 ways:
g++ -static program.cpp -lpthread
and
g++ program.cpp -ltpthread
When I look at the mtrace’s output (mem.out in my case)
I see the following when i use the -static option, mtrace reports:
Memory Not freed:
__________________
Address Size Caller
0x085ac350 0x88 program.cpp:0
But when I exclude the -static option, mtrace reports the glorious:
No memory leaks.
So any ideas as to whats going on here?
Here’s a recipe for reproducing this on my regular desktop Linux system (FC-17):
Compiled with
g++ -g -static -pthread. This is how I executed it to get themtraceerror:I have a 64 bit system, so the size doesn’t match. When I disassemble the address in
gdb, it gives this:So it looks like some thread local storage was allocated for the thread. It seems like a one time allocation per thread, because there are no additional allocations when I added a
pthread_createcall after the join, while there was one when I added it before the join. The_dl_allocate_tlssuggests this is normally a functioned called during dynamic linking, but it seems it is being called during thread stack initialization. A grep through theglibccode shows it is being called inallocate_stack.c.There does seem to be corresponding calls to
_dl_deallocate_tlsinglibc, so I think this error is harmless.valgrinddoes not pick up any memory leaks.