This is the mail archive of the
newlib@sourceware.org
mailing list for the newlib project.
[PATCH] Fix memory leak in dtoa/ldtoa
- From: Corinna Vinschen <vinschen at redhat dot com>
- To: newlib at sourceware dot org
- Date: Mon, 9 Oct 2006 19:47:03 +0200
- Subject: [PATCH] Fix memory leak in dtoa/ldtoa
- Reply-to: newlib at sourceware dot org
Hi,
the following report has been posted to the Cygwin list. A test application
using printf("%f") in short-lived threads uses up all memory. The test
application is very simple:
void *testThread(void *data)
{
printf("\r\n%f",1.232231212);
return(NULL);
}
int main (int argc, char *argv[])
{
pthread_t t;
while(1)
{
pthread_create(&t,NULL,testThread,NULL);
pthread_join(t,NULL);
}
return 1;
}
The cause is apparently a memory leak in ldtoa resp. dtoa. The first
time the function is called, _REENT_MP_RESULT(ptr) gets newly allocated
memory by a call to Balloc. This memory is Bfree'd in subsequent calls
to ldtoa/dtoa, but immediately re-Ballocated. Since a Ballocated area
is only actually freed in _reclaim_reent when a matching Bfree has been
called, the _REENT_MP_RESULT(ptr) memory is never actually freed and
every new thread using ldtoa/dtoa will leak this memory area when the
thread exits.
The solution is to explicitely free the _REENT_MP_RESULT(ptr) area in
_reclaim_reent. That's what the below patch does. Ok to apply?
Corinna
* libc/reent/reeent.c (_reclaim_reent): Free _REENT_MP_RESULT.
Index: libc/reent/reent.c
===================================================================
RCS file: /cvs/src/src/newlib/libc/reent/reent.c,v
retrieving revision 1.8
diff -u -p -r1.8 reent.c
--- libc/reent/reent.c 9 Sep 2004 19:46:54 -0000 1.8
+++ libc/reent/reent.c 9 Oct 2006 17:44:46 -0000
@@ -69,6 +69,8 @@ _DEFUN (_reclaim_reent, (ptr),
_free_r (ptr, _REENT_MP_FREELIST(ptr));
}
+ if (_REENT_MP_RESULT(ptr))
+ _free_r (ptr, _REENT_MP_RESULT(ptr));
#ifdef _REENT_SMALL
if (ptr->_emergency)
--
Corinna Vinschen
Cygwin Project Co-Leader
Red Hat