Bug 79526

Summary: A memory leak in pthread_rwlock_init()
Product: Base System Reporter: Kimmo Mustonen <tzimmo>
Component: threadsAssignee: freebsd-threads (Nobody) <threads>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: 5.3-RELEASE   
Hardware: Any   
OS: Any   

Description Kimmo Mustonen 2005-04-04 13:40:12 UTC
	Calling pthread_rwlock_init() leaks memory each time it is called.
        Example code
---8<---8<---
#include <pthread.h>

/* start using valgrind:
   valgrind --tool=memcheck -v --leak-check=yes --show-reachable=yes \
     --num-callers=100 ./leak_pthread_1
*/

int main(void)
{
  int i;
  pthread_rwlock_t lock;

  for (i = 0; i < 100; i++) {
    pthread_rwlock_init(&lock, NULL);
    pthread_rwlock_destroy(&lock);
  }

  return 0;
}
---8<---8<---
        and valgrind report
---8<---8<---
==801== 2012 bytes in 301 blocks are still reachable in loss record 1 of 1
==801==    at 0x3C033183: malloc (in /usr/local/lib/valgrind/vgpreload_memcheck.so)
==801==    by 0x3C03A8D0: (within /usr/local/lib/valgrind/libpthread.so.1)
==801==    by 0x3C03BF49: pthread_mutex_init (in /usr/local/lib/valgrind/libpthread.so.1)
==801==    by 0x3C03BFA9: pthread_mutex_lock (in /usr/local/lib/valgrind/libpthread.so.1)
==801==    by 0x3C03F174: (within /usr/local/lib/valgrind/libpthread.so.1)
==801==    by 0x3C03F2D9: pthread_rwlock_init (in /usr/local/lib/valgrind/libpthread.so.1)
==801==    by 0x8048609: main (leak_pthread_1.c:14)
==801== 
---8<---8<---

Fix: 

Haven't looked at the sources, but probably easy to fix.
        I suppose pthread_rwlock_destroy() just does not free all the
        resources pthread_rwlock_init() allocates.
How-To-Repeat: 	Run the example code using valgrind.
Comment 1 Craig Rodrigues 2005-04-04 21:25:40 UTC
On Mon, Apr 04, 2005 at 03:36:02PM +0300, Kimmo Mustonen wrote:
>         Haven't looked at the sources, but probably easy to fix.
>         I suppose pthread_rwlock_destroy() just does not free all the
>         resources pthread_rwlock_init() allocates.


Can you try this patch?
prwlock was being allocated by malloc inside pthread_rwlock_init(),
but never freed.


--- lib/libpthread/thread/thr_rwlock.c.orig	Mon Apr  4 16:22:16 2005
+++ lib/libpthread/thread/thr_rwlock.c	Mon Apr  4 16:23:26 2005
@@ -131,6 +131,7 @@
 				prwlock->blocked_writers = 0;
 
 				*rwlock = prwlock;
+				free(prwlock);
 			}
 		}
 	}


-- 
Craig Rodrigues        
rodrigc@crodrigues.org
Comment 2 Kimmo Mustonen 2005-04-05 10:14:17 UTC
On Mon, 4 Apr 2005, Craig Rodrigues wrote:

> Can you try this patch?
> prwlock was being allocated by malloc inside pthread_rwlock_init(),
> but never freed.
> 				*rwlock = prwlock;
> +				free(prwlock);

First, this patch is broken. It sets the pointer to a datastructure but 
frees the contents of the structure immediately after that, later causing 
referencing to deallocated memory and crashing.

The valgrind version did not crash but still leaked memory after this 
(broken) patch. I didn't realise valgrind has its own pthread library. 
Thus, the problem is most probably in the valgrind implementation of 
pthread library and the actual pthread library does not leak. So I don't 
think there's a need for a patch in pthread.

I hope this problem will disappear when the new valgrind using native 
pthread library is ported to FreeBSD.

Regards,
Kimmo Mustonen
Comment 3 David Xu freebsd_committer freebsd_triage 2005-04-29 15:03:35 UTC
State Changed
From-To: open->closed

It is not a bug of thread library, it should be fixed in valgrind ports.