title: boost::mutex, pthread_mutex_destroy failed - debug suggestions?
body: <p>We have several locks (boost::mutex) in static classes, but when the program exits, pthread_mutex_destroy fails in the destructor of the mutex (there is an assertion checking this in boost).</p>
  
  <p>As far as I know, pthread_mutex_destroy will only fail in two cases.</p>
  
  <pre><code>[EBUSY]  The implementation has detected an attempt to destroy the object referenced by mutex   while it is locked or referenced (for example, while being used in a pthread_cond_timedwait() or pthread_cond_wait()) by another thread.
  [EINVAL] The value specified by mutex is invalid.
  </code></pre>
  
  <p>When I run in GDB and I print the lock I see that it is unlocked.
   Unfortunately I'm having trouble printing errno in GDB.</p>
  
  <pre><code>#3  0x000000000044a2c6 in ~mutex (this=0x847840, __in_chrg=&lt;value optimized out&gt;) at /usr/include/boost/thread/pthread/mutex.hpp:47
  47              BOOST_VERIFY(!pthread_mutex_destroy(&amp;m));
  (gdb) p m
  $1 = {__data = {__lock = 0, __count = 0, __owner = 0, __nusers = 4294967294, __kind = 0, __spins = 0, __list = {__prev = 0x0, 
        __next = 0x0}}, __size = '\000' &lt;repeats 12 times&gt;"\376, \377\377\377", '\000' &lt;repeats 23 times&gt;, __align = 0}
  </code></pre>
  
  <p>Now that I am writing this post the value of __nusers and __size look strange. This could hint to the lock being invalid, but I know that the lock was valid at some point (I wrap the boost::mutex in a Lock class, where I printed the value of this(0x847840) in the constructor, destructor and lock/unlock functions.</p>
  
  <p>Any help as to how to debug this would be greatly appreciated.</p>
  
  <p><strong>Edit</strong>
  The Locks class inherits from boost::mutex, and exports a scopedlock (from memory):</p>
  
  <pre><code>lock_type::scoped_lock getScopedLock() {
    return lock_type::scoped_lock( *this );
  }
  </code></pre>
  
  <p>I've also tried to add the lock as a member, instead of inheriting from it, with no change in behavior.
  I do not think that the getScopedLock function could introduce any problems(the scoped lock is returned y value, but a copy is not made because of RVO), but thought it could be worth mentioning.
  It is used as follows (we are using c++0x):</p>
  
  <pre><code>auto lock = lock_.getScopedLock();
  </code></pre>
  
  <p>The complete stracktrace:</p>
  
  <pre><code>(gdb) where
  #0  0x00007ffff559da75 in *__GI_raise (sig=&lt;value optimized out&gt;) at ../nptl/sysdeps/unix/sysv/linux/raise.c:64
  #1  0x00007ffff55a15c0 in *__GI_abort () at abort.c:92
  #2  0x00007ffff5596941 in *__GI___assert_fail (assertion=0x55851c "!pthread_mutex_destroy(&amp;m)", file=&lt;value optimized out&gt;, line=47, 
      function=0x5595a0 "boost::mutex::~mutex()") at assert.c:81
  #3  0x000000000044a2c6 in ~mutex (this=0x847840, __in_chrg=&lt;value optimized out&gt;) at /usr/include/boost/thread/pthread/mutex.hpp:47
  #4  0x000000000044d923 in ~Lock (this=0x847840, __in_chrg=&lt;value optimized out&gt;) at include/Locks.h:43
  #5  0x00007ffff55a3262 in __run_exit_handlers (status=0) at exit.c:78
  #6  *__GI_exit (status=0) at exit.c:100
  #7  0x00000000004ea9a6 in start () at src/main.cc:191
  #8  0x00000000004de5aa in main (argc=1, argv=0x7fffffffe7b8) at src/main.cc:90
  </code></pre>