Skip to content

Latest commit

 

History

History
79 lines (61 loc) · 1.86 KB

File metadata and controls

79 lines (61 loc) · 1.86 KB
description Learn more about: C26160
title C26160
ms.date 11/04/2016
ms.topic reference
f1_keywords
C26160
helpviewer_keywords
C26160
ms.assetid e6518687-36b4-4eae-a732-758881638295

C26160

warning C26160: Caller possibly failing to hold lock <lock> before calling function <func>.

Warning C26160 resembles warning C26110 except that the confidence level is lower. For example, the function may contain annotation errors.

Examples

The following code generates 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 FuncInitCallBad() // No annotation provided, analyzer generates warning
    {
        FuncNeedsLock(); // Warning C26160
    }

};

The following code shows a solution to the previous example.

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