[Muscle] thread_unix.c`SYS_ThreadCreate() leaks memory

Paul Klissner Paul.Klissner at Sun.COM
Sat Aug 18 11:00:28 PDT 2007


We found that under stress testing the 'launcher ' (a new daemon used to 
fire up
multiple instances) was leaking about 1Gb / day, when multiple threads were
calling SCardEstablishContext()/SCardReleaseContext() in a fast loop.

It took awhile to find the source of that leak.

libumem analysis showed mmap() calls leaked, but didn't show who the mmap()
caller was.  DTrace was used to determine that the call that leaked it 
was pthread_attr_init().

The bug is that SYS_ThreadCreate(), the PCSClite wrapper for the POSIX 
thread library,
calls pthread_attr_init(), which allocates memory,  without the calling 
the counterpart
call, pthread_attr_destroy() to release the memory.  And the thread 
library calls mmap()
instead of malloc() to grow memory (at least indirectly).

Adding a pthread_attr_destroy() call right after pthread_create() fixes 
it (after some
re-arrangement of the 'if' statement).

INTERNAL *int* *SYS_ThreadCreate*(PCSCLITE_THREAD_T * pthThread, *int* attributes,
        PCSCLITE_THREAD_FUNCTION(pvFunction), LPVOID pvArg)
{
        pthread_attr_t attr;

        *if* (0 != pthread_attr_init(&attr))
                *return* FALSE;

        *if* (0 != pthread_attr_setdetachstate(&attr,
                attributes & THREAD_ATTR_DETACHED ? PTHREAD_CREATE_DETACHED : PTHREAD_CREATE_JOINABLE))
                *return* FALSE;

        *if* (0 == pthread_create(pthThread, &attr, pvFunction, pvArg))
                *return* TRUE;
        *else*
                *return* FALSE;


Paul



More information about the Muscle mailing list