Disallowing creation of the temporary objects

后端 未结 8 947
南方客
南方客 2020-11-29 09:01

While debugging crash in a multithreaded application I finally located the problem in this statement:

CSingleLock(&m_criticalSection, TRUE);
8条回答
  •  一个人的身影
    2020-11-29 09:48

    Compiler shouldn't disallow temporary object creation, IMHO.

    Specially cases like shrinking a vector you really need temporary object to be created.

    std::vector(v).swap(v);
    

    Though it is bit difficult but still code review and unit testing should catch these issues.

    Otherwise, here is one poor man's solution:

    CSingleLock aLock(&m_criticalSection); //Don't use the second parameter whose default is FALSE
    
    aLock.Lock();  //an explicit lock should take care of your problem
    

提交回复
热议问题