다음을 통해 공유


C26160

경고 C26160: 호출자가 <lock> 잠금을 보유 하도록 가능한 경우 실패 <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);
    }
};