[asterisk-dev] Thread-local storage in linked lib cause memory leak

Tilghman Lesher tlesher at digium.com
Sun Jul 18 10:45:07 CDT 2010


On Sunday 18 July 2010 06:15:08 Alex Massover wrote:
> Hi,
>
> I have a custom module that linked to external library. Inside that library
> there's following declaration:
>
> #define HOSTBUF_SIZE 1024
> static __thread char tmphostbuf[HOSTBUF_SIZE];
>
> I.e. there's buffer stored in thread local storage.
>
> When I link this library to my unit test program (also multi-threaded)
> everything is OK, and there's no memory leaks in unit test.
>
> But when I link this library to my custom Asterisk module, there's memory
> leak. I can observe the memory leak and valgrind confirms that there's
> memory leak in the function that uses this buffer, and the leak is exactly
> 1024 bytes per call (thread), and there's no leak if I don't call the
> function that uses this buffer. So it's for sure that this buffer causes
> the leak. Somehow this buffer is neither freed no reused.
>
> AFAIK buffer in TLS does not supposed to leak and it doesn't leak when the
> lib is linked to my unit test program. But it leaks when the lib is linked
> to asterisk module. This module does nothing special (no new threads
> inside, no locks etc.).
>
>
> Does Asterisk do something tricky with threads and that can break TLS in
> linked lib?
>
> It would be great if somebody could enlighten me on the subj. The issue
> might be relevant in general to the libs Asterisk is linked to, not only to
> my specific example.

Well, if the thread does not exit, that would explain why the memory is not
freed.  Memory in thread-local storage does not get freed until the thread
using it exits.  And it will not "reuse" that memory either -- that's what
threadlocal means.

Otherwise, your description is way too vague for us to be able to help you on
this.  Seeing the code is the only way we're going to be able to do anything
other than guess.

-- 
Tilghman Lesher
Digium, Inc. | Senior Software Developer
twitter: Corydon76 | IRC: Corydon76-dig (Freenode)
Check us out at: www.digium.com & www.asterisk.org



More information about the asterisk-dev mailing list