C26160
警告 C26160:调用方可能未能持有锁 <lock>(在调用函数 <func> 前)。
警告 C26160 类似于警告 C26110,只不过信心更低。例如,函数可以包含注释错误。
示例
下面的代码生成警告 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
}
};
下面的代码演示一解决方案到上。
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);
}
};