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

Alex Massover alex at jajah.com
Tue Jul 20 02:45:41 CDT 2010


Hi, 

Thank you for the reply. The problematic code in the library is:

#define HOSTBUF_SIZE 1024
static __thread	char tmphostbuf[HOSTBUF_SIZE];

struct hostent *rc_gethostbyname(const char *hostname)
{
        struct  hostent *hp;
        struct  hostent hostbuf;
        int     res;
        int     herr;
        while ((res = gethostbyname_r(hostname, &hostbuf, tmphostbuf, HOSTBUF_SIZE, &hp, &herr)) == ERANGE)
        {
                return NULL;
        }
        if(res) return NULL;
        if (hp == NULL) {
                return NULL;
        }
        return hp;
}

Looks like tmphostbuf is never freed, despite that thread exits (call ends and thread exits). If threads were not exiting the system would run of the threads (I run hundreds of thousands of calls). 

I fixed the problem by looking how ast_gethostbyname() is implemented and doing the same. It doesn't leak for me anymore. 

But I still don't understand why TLS implementation above leaks when linked to Asterisk. Maybe I'll create a simple lib, module and dialplan to demonstrate the behavior.


> -----Original Message-----
> From: asterisk-dev-bounces at lists.digium.com [mailto:asterisk-dev-
> bounces at lists.digium.com] On Behalf Of Tilghman Lesher
> Sent: יום א 18 יולי 2010 18:45
> To: Asterisk Developers Mailing List
> Subject: Re: [asterisk-dev] Thread-local storage in linked lib cause
> memory leak
> 
> 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
> 
> --
> _____________________________________________________________________
> -- Bandwidth and Colocation Provided by http://www.api-digital.com --
> 
> asterisk-dev mailing list
> To UNSUBSCRIBE or update options visit:
>    http://lists.digium.com/mailman/listinfo/asterisk-dev
> 
> This mail was received via Mail-SeCure System.
> 


This mail was sent via Mail-SeCure System.





More information about the asterisk-dev mailing list