C26160
警告 C26160: 呼叫端在呼叫函式<func>之前無法維持鎖定 <lock> 。
警告 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);
}
};