C26160

avertissement C26160 : Appelant ne parvenant peut-être pas à maintenir le verrou<lock> avant l'appel de la fonction <func>.

L'avertissement C26160 ressemble à l'acertissement C26110, excepté que le niveau de confiance est plus bas.Par exemple, la fonction peut contenir des erreurs d'annotation.

Exemple

Le code suivant génère des avertissements 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
    }

};

Le code suivant illustre une solution à l'exemple précédent.

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