我需要释放bitpointer
,因为该函数已多次执行并且内存使用率由于某种原因我不了解,并且在达到ram使用率22mb之后崩溃。.如果我尝试删除bitpointer
或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
}
最佳答案
在SelectObject中,保存返回的值:HGDIOBJ save = SelectObject(hdcTemp, hBitmap2);
然后在ReleaseDC之前,删除DIB DeleteObject( SelectObject(hdcTemp, save) );
否则,您将创建位图并且不要删除它们。