【发布时间】:2015-08-28 02:53:40
【问题描述】:
我需要释放bitpointer,因为这个函数被执行了多次并且内存使用量因为我不明白的原因而增长,并且它在达到 22mb 的内存使用量后崩溃。如果我尝试删除@ 987654322@ 像这样delete []bitpointer 或free(bitpointer)我得到访问冲突错误。但我不明白为什么,因为函数不应该再使用指针,并且设置了新的红蓝绿值..
void Get_Color(int x,int y,int w,int h,int &red,int &green,int &blue,int action)
{
HDC hdc, hdcTemp;
RECT rect;
BYTE*bitPointer=new BYTE[4*h*w];
HWND Desktop = GetDesktopWindow();
hdc = GetDC(Desktop);
GetWindowRect(Desktop, &rect);
hdcTemp = CreateCompatibleDC(hdc);
BITMAPINFO bitmap;
bitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
bitmap.bmiHeader.biWidth = w;
bitmap.bmiHeader.biHeight = h;
bitmap.bmiHeader.biPlanes = 1;
bitmap.bmiHeader.biBitCount = 32;
bitmap.bmiHeader.biCompression = BI_RGB;
bitmap.bmiHeader.biSizeImage = 0;
bitmap.bmiHeader.biClrUsed = 0;
bitmap.bmiHeader.biClrImportant = 0;
HBITMAP hBitmap2 = CreateDIBSection(hdcTemp, &bitmap, DIB_RGB_COLORS, (void**)(&bitPointer), NULL, NULL);
SelectObject(hdcTemp, hBitmap2);
BitBlt(hdcTemp, 0, 0, w, h, hdc, x, y, SRCCOPY);
if(action==1)
{
for(int j=0;j<=w*h*4;j+=4)
{
red = bitPointer[j+2];
green = bitPointer[j+1];
blue = bitPointer[j];
if(red<30 && green>190 && blue>190)
{
break;
}
}
}
else
{
for(int j=0;j<=w*h*4;j+=4)
{
red = bitPointer[j+2];
green = bitPointer[j+1];
blue = bitPointer[j];
break;
}
}
///RELEASE
ReleaseDC(NULL,hdc);
ReleaseDC(NULL,hdcTemp);
delete []bitPointer; ///Error
}
【问题讨论】:
-
确切的错误信息..
-
my.exe 0xWHATEVA 中 0xWHATEVA (msvcr110d.dll) 处的未处理异常:访问冲突读取位置 0xWHATEVA 我可以中断或继续。
标签: c++ memory free allocation delete-operator