Share via


C26165

warning C26165: Probabilmente non riesce a rilasciare il blocco <lock> nella funzione <func>.

L'avviso C26165 è simile all'avviso C26115 eccetto che il livello di fiducia è inferiore.Ad esempio, la funzione può contenere errori di annotazione.

Esempio

Il codice seguente genera l'avviso C26165.

_Create_lock_level_(LockLevelOne); 
_Create_lock_level_(LockLevelTwo); 

struct LockLevelledStruct
{
    _Has_lock_level_(LockLevelOne) CRITICAL_SECTION a;
    _Has_lock_level_(LockLevelTwo) CRITICAL_SECTION b;
};

_Lock_level_order_(LockLevelOne, LockLevelTwo);

_Acquires_lock_(s->b) void GetLockFunc(LockLevelledStruct* s)
{
    EnterCriticalSection(&s->b);
}

void testLockLevelledStruct(LockLevelledStruct* s) // Warning C26165
{
    EnterCriticalSection(&s->a); 
    GetLockFunc(s);
    LeaveCriticalSection(&s->a);
}

Per risolvere il problema, modificare l'esempio precedente con il seguente.

_Create_lock_level_(LockLevelOne); 
_Create_lock_level_(LockLevelTwo); 

struct LockLevelledStruct
{
    _Has_lock_level_(LockLevelOne) CRITICAL_SECTION a;
    _Has_lock_level_(LockLevelTwo) CRITICAL_SECTION b;
};

_Lock_level_order_(LockLevelOne, LockLevelTwo);

_Acquires_lock_(s->b) void GetLockFunc(LockLevelledStruct* s)
{
    EnterCriticalSection(&s->b);
}

_Releases_lock_(s->b) void ReleaseLockFunc(LockLevelledStruct* s)
{
    LeaveCriticalSection(&s->b);
}

void testLockLevelledStruct(LockLevelledStruct* s) // OK
{
    EnterCriticalSection(&s->a); 
    GetLockFunc(s);
    ReleaseLockFunc(s);
    LeaveCriticalSection(&s->a);
}