Edit

Share via


Warning C26116

Failing to acquire or to hold lock 'lock' in 'func'.

Enforcement of syntactically scoped lock acquire and lock release pairs in C/C++ programs isn't performed by the language. A function may introduce a locking side effect by making an observable modification to the concurrency state. For example, a lock wrapper function increments the number of lock acquisitions, or lock count, for a given lock. You can annotate a function that has a side effect from a lock acquire or lock release by using _Acquires_lock_ or _Requires_lock_held, respectively. Without such annotations, a function is expected not to change any lock count after it returns. If acquires and releases aren't balanced, they're considered to be orphaned. Warning C26116 is issued when a function has been annotated with _Acquires_lock_, but it doesn't acquire a lock, or when a function is annotated with _Requires_lock_held and releases the lock.

Example

The following example generates warning C26116 because the function DoesNotLock was annotated with _Acquires_lock_ but doesn't acquire it. The function DoesNotHoldLock generates the warning because it's annotated with _Requires_lock_held and doesn't hold it.

typedef struct _DATA
{
    CRITICAL_SECTION cs;
} DATA;

_Acquires_lock_(p->cs) void DoesLock(DATA* p)
{
    EnterCriticalSection(&p->cs); // OK
}

_Acquires_lock_(p->cs) void DoesNotLock(DATA* p)
{
    // Warning C26116
}

_Requires_lock_held_(p->cs) void DoesNotHoldLock(DATA* p)
{
    LeaveCriticalSection(&p->cs); // Warning C26116
}

See also