C26135

warning C26135: Missing annotation <annotation> at function <func>.

Warning C26135 is issued when the analyzer infers that a function is a lock wrapper function that has a lock acquire or lock release side effect. If the code is not intended to be a wrapper function, then either the lock is leaking (if the lock is being acquired) or it is being released incorrectly (if the lock is being released).

Example

The following example generates warning C26135 because an appropriate side effect annotation is missing.

    typedef struct _DATA 
    {
        CRITICAL_SECTION cs;
    } DATA;

    void MyEnter(DATA* p) 
    {
        // Warning C26135:
        // Missing side effect annotation _Acquires_lock_(&p->cs)
        EnterCriticalSection(&p->cs);
    }

    void MyLeave(DATA* p) 
    {
        // warning C26135:
        // Missing side effect annotation _Releases_lock_(&p->cs)
        LeaveCriticalSection(&p->cs);
    }

Warning C26135 is also issued when a conditional locking side effect is detected. To annotate a conditional effect, use the _When_(ConditionExpr, LockAnnotation) annotation, where LockAnnotation is either _Acquires_lock_ or _Releases_lock_ and the predicate expression ConditionExpr is a Boolean conditional expression. The side effects of other annotations on the same function only occur when ConditionExpr evaluates to true. Because ConditionExpr is used to relay the condition back to the caller, it must involve variables that are recognized in the calling context. These include function parameters, global or class member variables, or the return value. To see the return value, use a special keyword in the annotation, return, as shown in the following example.

    typedef struct _DATA 
    {
        CRITICAL_SECTION cs; 
        int state;
   } DATA;

    _When_(return != 0, _Acquires_lock_(p->cs))
    int TryEnter(DATA* p) 
    {
        if (p->state != 0) 
        {
            EnterCriticalSection(&p->cs);
            return p->state;
        }

        return 0;
    }

For shared/exclusive locks, also known as reader/writer locks, you can express locking side effects by using the following annotations:

  • _Acquires_shared_lock_(LockExpr)

  • _Releases_shared_lock_(LockExpr)

  • _Acquires_exclusive_lock_(LockExpr)

  • _Releases_exclusive_lock_(LockExpr)