我正在运行一个函数,它获取桌面的屏幕截图,然后创建一个从中心直线向上的像素 RGB 值的向量数组,但运行 30 秒 - 2 分钟后,具体取决于系统内存量正在使用。它抛出异常“访问冲突读取位置 0x0000000000000002”。这个函数在 main 中的无限 while 循环中被调用随着程序不断运行,我的计算机也运行得越来越慢。 Visual Studio 还显示进程内存约为 88 MB。例外是在这行代码
pixelsStraightdown.push_back(RGB{ red, green, blue });
这里是完整的功能
std::vector<RGB> getPixelsAboveCenter(const Image& image) {
std::vector<RGB> pixelsStraightUp(image.height/2);
double centerX = image.width / 2;
double centerY = image.height / 2;
centerX = round(centerX);
centerY = round(centerY);
int startY = 0;
int endY = centerY;
startY = max(0, startY);
endY = min(endY, image.height - 1);
for (int y = startY; y <= endY; y++) {
double rowOffset = y * image.rowPitch;
double offset = rowOffset + (centerX * 4);
uint8_t blue = image.bytes[offset];
uint8_t green = image.bytes[offset + 1];
uint8_t red = image.bytes[offset + 2];
pixelsStraightUp.push_back(RGB{ red, green, blue });
}
return pixelsStraightUp;
}
std::vector<RGB> getPixelsBelowCenter(const Image& image) {
std::vector<RGB> pixelsStraightdown(image.height / 2);
double centerX = image.width / 2;
double centerY = image.height / 2;
centerX = round(centerX);
centerY = round(centerY);
int startY = image.height - 1;
int endY = centerY;
startY = max(endY, startY);
endY = min(endY, image.height - 1);
for (int y = startY; y >= endY; y--) {
double rowOffset = y * image.rowPitch;
double offset = rowOffset + (centerX * 4);
uint8_t blue = image.bytes[offset];
uint8_t green = image.bytes[offset + 1];
uint8_t red = image.bytes[offset + 2];
pixelsStraightdown.push_back(RGB{ red, green, blue });
}
return pixelsStraightdown;
}
我不知道从哪里开始解决这个问题,并且还没有尝试过任何事情,堆栈跟踪什么也没有显示,并且调试不会显示任何变量在异常发生时等于什么。