Skip site navigation (1)Skip section navigation (2)
Date:      Wed, 20 May 2009 14:20:42 -0500
From:      Dan Nelson <dnelson@allantgroup.com>
To:        Peter Steele <psteele@maxiscale.com>
Cc:        #freebsd-questions <freebsd-questions@freebsd.org>
Subject:   Re: pthread_detach doesn't release memory
Message-ID:  <20090520192042.GG52703@dan.emsphone.com>
In-Reply-To: <19206470.1651242845780966.JavaMail.HALO$@halo>
References:  <19206470.1651242845780966.JavaMail.HALO$@halo>

next in thread | previous in thread | raw e-mail | index | archive | help
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@allantgroup.com



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?20090520192042.GG52703>