Bug 134376

Summary: pthread(3): pthread manpages don't mention that PTHREAD_MUTEX_INITIALIZER may leak memory
Product: Documentation Reporter: Török Edwin <edwintorok>
Component: Books & ArticlesAssignee: freebsd-doc (Nobody) <doc>
Status: Closed FIXED    
Severity: Affects Only Me    
Priority: Normal    
Version: Latest   
Hardware: Any   
OS: Any   

Description Török Edwin 2009-05-08 20:30:03 UTC
The manpages for pthread(3), pthread_mutex_lock(5), pthread_mutex_init(3), pthread_mutex_destroy(3) make no mention of PTHREAD_MUT
EX_INITIALIZER, and that using it in a loop causes memory leaks.                                                                          
It  is obvious that PTHREAD_MUTEX_INITIALIZER acts like pthread_mutex_init, which on FreeBSD allocates memory.                    
If pthread_mutex_destroy is not called this results in a 60 byte memory leak (on amd64), which in turn can cause long running daemons to r
un out of memory as described here: https://wwws.clamav.net/bugzilla/show_bug.cgi?id=1567#c33, and here: https://wwws.clamav.net/bugzilla/show_bug.cgi?id=1567#c32                                                                                 
 
        Although portable programs shouldn't rely on knowing the implementation details of pthreads, knowing that it allocates memory can
(and maybe other implementation details) can provide useful debugging hints when something goes wrong.

Fix: 

PTHREAD_MUTEX_INITIALIZER doesn't appear in any of the pthread manpages, it should be added.                                 
        The FreeBSD manpages for pthread_mutex_lock, or pthread_mutex_init should document that on FreeBSD memory is allocated when these 
functions are used (I was not aware of this for example), and that one shouldn't use PTHREAD_MUTEX_INITIALIZER in code that is possibly ca
lled in a loop. Since you can't pthread_mutex_destroy a mutex "allocated" with PTHREAD_MUTEX_INITIALIZER, the manpage should recommend the
use of pthread_mutex_init/pthread_mutex_destroy in this case.
        By reading the /usr/include/pthread.h file I realize that PTHREAD_MUTEX_INITIALIZER is actually NULL, so I guess memory allocation
 happens on first pthread_mutex_lock call, maybe the manpage should document that.
How-To-Repeat: Assign PTHREAD_MUTEX_INITIALIZER to a variable in a function, call pthread_mutex_lock/unlock, call function in a loop ->          
there is a 60-byte memory leak. Look at pthread manpages for clues about what can be wrong, find no clues.
Comment 1 Benedict Reuschling freebsd_committer freebsd_triage 2010-07-09 22:41:37 UTC
State Changed
From-To: open->closed

I talked with das@ about this issue. According to him, the pthread man page 
is currently not written with the goal of teaching people how to use pthreads. 
The addition you're proposing would be a little out of place, perhaps a  
good book about that subject would be a better way to gain such knowledge. 
If anyone is willing to write much more detailed man pages on pthreads, we 
would be glad for this contribution. But as far as this PR is concerned, 
we agree that it would be best to close it for now.