pthread_detach doesn't release memory

Dan Nelson dnelson at allantgroup.com
Wed May 20 19:20:48 UTC 2009


In the last episode (May 20), Peter Steele said:
> I create a thread using something like this: 
> 
> pthread_t thread; 
> pthread_create(&thread, NULL, mythread, NULL); 
> pthread_detach(thread); 
> 
> I use the detach because I want to make sure the thread's resource are
> reclaimed when the thread completes.  However, this does not seem to work. 
> Each time I create a thread the application's memory footprint grows by
> 128 bytes and this memory is never released.

Are you malloc'ing some data in your thread and not freeing it before
exiting?  pthread_detach simply lets the system discard the thread when it
exits instead of preserving the return code (for pthread_join to retrieve). 
It isn't responsible for freeing all memory allocated by the thread, and it
shouldn't, since one thread may allocate memory that another thread frees
later.

-- 
	Dan Nelson
	dnelson at allantgroup.com


More information about the freebsd-questions mailing list