C26160

警告 C26160: 関数 <func> の呼び出し前に、呼び出し元でロック <ロック> の保持に失敗する可能性があります

警告 C26160 で警告の C26110 に似ていますが、信頼レベルの低いものです。たとえば、関数は、注釈のエラーが含まれる場合があります。

使用例

次のコードでは、警告 C26160 を生成します。

struct Account
{
    _Guarded_by_(cs) int balance;
    CRITICAL_SECTION cs;

    _No_competing_thread_ void Init() 
    {
        balance = 0; // OK
    }

    _Requires_lock_held_(this->cs) void FuncNeedsLock();

    _No_competing_thread_ void FuncInitCallOk()
        // this annotation requires this function is called 
        // single-threaded, therefore we don't need to worry 
        // about the lock
    {
        FuncNeedsLock(); // OK, single threaded
    } 

    void FuncInitCallBad() // No annotation provided, analyzer generates warning
    {
        FuncNeedsLock(); // Warning C26160
    }

};

次のコードは、前の例にソリューションを示します。

struct Account
{
    _Guarded_by_(cs) int balance;
    CRITICAL_SECTION cs;

    _No_competing_thread_ void Init()
    {
        balance = 0; // OK
    }

    _Requires_lock_held_(this->cs) void FuncNeedsLock();

    _No_competing_thread_ void FuncInitCallOk()
        // this annotation requires this function is called 
        // single-threaded, therefore we don't need to worry 
        // about the lock
    {
        FuncNeedsLock(); // OK, single threaded
    } 

    void FuncInitCallBadFixed() // this function now properly acquires (and releases) the lock
    {
        EnterCriticalSection(&this->cs); 
        FuncNeedsLock();  
        LeaveCriticalSection(&this->cs);
    }
};