程序正常运行几分钟后,ReadFile开始失败,错误代码为ERROR_WORKING_SET_QUOTA。
我使用带有重叠I/O的ReadFile,如下所示:
while (continueReading)
{
BOOL bSuccess = ReadFile(deviceHandle, pReadBuf, length,
&bytesRead, readOverlappedPtr);
waitVal = WaitForMultipleObjects(
(sizeof(eventsToWaitFor)/sizeof(eventsToWaitFor[0])),
eventsToWaitFor, FALSE, INFINITE);
if (waitVal == WAIT_OBJECT_0) {
// do stuff
} else if (waitVal == WAIT_OBJECT_0 + 1) {
// do stuff
} else if (waitVal == WAIT_OBJECT_0 + 2) {
// complete the read
bSuccess = GetOverlappedResult(deviceHandle, &readOverlapped,
&bytesRead, FALSE);
if (!bSuccess) {
errorCode = GetLastError();
printf("ReadFile error=%d\n", errorCode);
}
}
}为什么我会得到这个错误?
发布于 2010-08-10 01:12:44
问题是调用ReadFile的次数比调用GetOverlappedResult的次数多。导致进程耗尽用于处理所有未完成读取的资源。
此外,我们应该检查ReadFile的结果,并确保结果为ERROR_IO_PENDING,如果不是并且ReadFile返回FALSE,那么就有另一个问题。
确保每次成功调用ReadFile时都调用一次GetOverlappedResult。如下所示:
BOOL bPerformRead = TRUE;
while (continueReading)
{
BOOL bSuccess = TRUE;
// only perform the read if the last one has finished
if (bPerformRead) {
bSuccess = ReadFile(deviceHandle, pReadBuf, length,
&bytesRead, readOverlappedPtr);
if (!bSuccess) {
errorCode = GetLastError();
if (errorCode != ERROR_IO_PENDING) {
printf("ReadFile error=%d\n", errorCode);
return;
}
} else {
// read completed right away
continue;
}
// we can't perform another read until this one finishes
bPerformRead = FALSE;
}
waitVal = WaitForMultipleObjects(
(sizeof(eventsToWaitFor)/sizeof(eventsToWaitFor[0])),
eventsToWaitFor, FALSE, INFINITE);
if (waitVal == WAIT_OBJECT_0) {
// do stuff
} else if (waitVal == WAIT_OBJECT_0 + 1) {
// do stuff
} else if (waitVal == WAIT_OBJECT_0 + 2) {
// complete the read
bSuccess = GetOverlappedResult(deviceHandle, &readOverlapped,
&bytesRead, FALSE);
// the read is finished, we can read again
bPerformRead = TRUE;
if (!bSuccess) {
errorCode = GetLastError();
printf("GetOverlappedResult from ReadFile error=%d\n", errorCode);
}
}
}https://stackoverflow.com/questions/3442385
复制相似问题