Search code examples
c++mutexcritical-section

What is wrong with this tiny piece of mutex code?


// A Mutex allows threads mutually exclusive access to a resource.
//-----------------------------------------------------------------------

class Mutex
{
private:
    CRITICAL_SECTION m_mutex;

public:
     Mutex() { InitializeCriticalSection(&m_mutex); }
    ~Mutex() { DeleteCriticalSection(&m_mutex);     }

    void acquire() { EnterCriticalSection(&m_mutex); }
    void release() { LeaveCriticalSection(&m_mutex); }
};

Using the Entrek Codesnitch software to debug and test for any memory leaks, etc., it reports the following error:

InitializeCriticalSection Error: lpCriticalSection (0x000387d4) points to an invalid 
  memory location (0x00018984) Mutex::Mutex in lockmutex.h, line 29

Maybe all my sleepless nights are finally getting to me. But I don't understand what it's exactly complaining about. Any ideas?


Solution

  • I'll bet you can fake out the snitch with ::memset ( & m_mutex, 0, sizeof ( m_mutex ) ); before the call to init it.