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