共用方式為


C26165

C26165警告:可能無法釋放<func>在函式中<lock>的鎖定 。

警告 C26165 類似 C26115 警告,除了確定較低。 例如,函式可能包含附註錯誤。

範例

下列程式碼會產生警告 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);
}

若要更正這則警告,請將上述範例變更如下。

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