VC ++ 2010: Weird Critical Section Error

My program accidentally breaks into a small script that I can reproduce, but this happens in the mlock.c file (which is a VC ++ time file) from the ntdll.dll file, and I do not see the stack trace. However, I know that this happens in one of my stream functions.

This is the mlock.c code in which the program crashes:

void __cdecl _unlock (
        int locknum
        )
{
        /*
         * leave the critical section.
         */
        LeaveCriticalSection( _locktable[locknum].lock );
}

The error is an "invalid descriptor". If I look at locknum, this number is larger than the size of _locktable, so that makes sense.

This is apparently due to the use of the critical section. I use CRITICAL_SECTIONS in my thread through the CCriticalSection wrapper class and its associated RAII CGuard defender. Definitions for here to avoid even more confusion.

This is a thread function that crashes:

unsigned int __stdcall CPlayBack::timerThread( void * pParams ) {
#ifdef _DEBUG
    DRA::CommonCpp::SetThreadName( -1, "CPlayBack::timerThread" );
#endif
    CPlayBack * pThis = static_cast<CPlayBack*>( pParams );
    bool bContinue = true;
    while( bContinue ) {
        float m_fActualFrameRate = pThis->m_fFrameRate * pThis->m_fFrameRateMultiplier;
        if( m_fActualFrameRate != 0 && pThis->m_bIsPlaying ) {
            bContinue = ( ::WaitForSingleObject( pThis->m_hEndThreadEvent, static_cast<DWORD>( 1000.0f / m_fActualFrameRate ) ) == WAIT_TIMEOUT );
            CImage img;
            if( pThis->m_bIsPlaying && pThis->nextFrame( img ) )
                pThis->sendImage( img );
        }
        else
            bContinue = ( ::WaitForSingleObject( pThis->m_hEndThreadEvent, 10 ) == WAIT_TIMEOUT );
    }
    ::GetErrorLoggerInstance()->Log( LOG_TYPE_NOTE, "CPlayBack", "timerThread", "Exiting thread" );
    return 0;
}

CCriticalSection? CImage CCriticalSection, CGuard RAII. , CImage CSharedMemory, . CCriticalSection, . :

CImage::~CImage() {
    CGuard guard(m_csData);
    if( m_pSharedMemory != NULL ) {
        m_pSharedMemory->decrementUse();
        if( !m_pSharedMemory->isBeingUsed() ){
            delete m_pSharedMemory;
            m_pSharedMemory = NULL;
        }
    }
    m_cProperties.ClearMin();
    m_cProperties.ClearMax();
    m_cProperties.ClearMode();
}

CSharedMemory::~CSharedMemory() {
    CGuard guardUse( m_cs );
    if( m_pData && m_bCanDelete ){
        delete []m_pData;
    }
    m_use = 0;
    m_pData = NULL;
}

- ? ?

. : ~ CSharedMemory. - .

: CSharedMemory

+5
3

KISS rock roll all nite . , CSharedMemoryClass std::tr1::shared_ptr<BYTE> CCriticalSection, . CImage, , IMHO.

, , , std::tr1::shared_ptr, , ... !

+1

" " , ; , , .

RAII . , RAII Sepration Of Concerns, :

  • / CRITICAL_SECTION
  • / CRITICAL_SECTION

CS, , SoC , . , , /. , psudocode:

void WorkerThreadProc(CCriticalSection cs)
{
  cs.Enter();
  // MAGIC HAPPENS
  cs.Leave();
}

int main()
{
  CCriticalSection my_cs;
  std::vector<NeatStuff> stuff_used_by_multiple_threads;

  // Create 3 threads, passing the entry point "WorkerThreadProc"
  for( int i = 0; i < 3; ++i )
    CreateThread(... &WorkerThreadProc, my_cs);

  // Join the 3 threads...
  wait(); 
}

CCriticalSection , 4 . , , CRITICAL_SECTION . , .

, , . , , "" , ? shared_ptr, "" , , .

"" , . :

class CCriticalSection : public CRITICAL_SECTION
{
public:
  CCriticalSection(){ InitializeCriticalSection(this); }
  ~CCriticalSection() { DestroyCriticalSection(this); }
};

... ...

class CSLock
{
public:
  CSLock(CRITICAL_SECTION& cs) : cs_(cs) { EnterCriticalSection(&cs_); }
  ~CSLock() { LeaveCriticalSection(&cs_); }
private: 
  CRITICAL_SECTION& cs_;
};

CCriticalSection, , const, CSLocks. CSLock , , , CCriticalSection ; .

+5
  • , Critical Section #pragma 1 ( , ).
  • , ( ) CS. , , .
  • , , .
+1

All Articles