Share via


C26160

경고 C26160: 함수 <func>를 호출하기 전에 호출자가 잠금 <lock> 유지에 실패할 수 있습니다.

경고 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);
    }
};