我正在使用 CreateDIBitmap() 并始終通過 DeleteObject() 實作它。當我的滑塊移動時我正在使用它,并且我注意到使用的記憶體每次都在增加。
這是我從索引位圖資料創建 HBITMAP 的函式:
HBITMAP APP_Make_HBITMAP_From_Bitmap_Indexed(int _width, int _height, u_int32* _table_ptr, u_int8* _index_ptr)
{
// window bitamp structure - to use with window API
struct sAPP_Windows_Bitmap
{
BITMAPINFOHEADER bmiHeader;
RGBQUAD bmiColors[256];
};
sAPP_Windows_Bitmap tmp_w_bitmap;
tmp_w_bitmap.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
tmp_w_bitmap.bmiHeader.biWidth = _width;
tmp_w_bitmap.bmiHeader.biHeight = -1 * _height;
tmp_w_bitmap.bmiHeader.biPlanes = 1;
tmp_w_bitmap.bmiHeader.biBitCount = 8;
tmp_w_bitmap.bmiHeader.biCompression = BI_RGB;
tmp_w_bitmap.bmiHeader.biSizeImage = 0;
tmp_w_bitmap.bmiHeader.biXPelsPerMeter = 0;
tmp_w_bitmap.bmiHeader.biYPelsPerMeter = 0;
tmp_w_bitmap.bmiHeader.biClrUsed = 256;
tmp_w_bitmap.bmiHeader.biClrImportant = 0;
for (unsigned int i = 0; i < 256; i )
{
tmp_w_bitmap.bmiColors[i].rgbRed = (_table_ptr[i] >> 8) & 0x0ff;
tmp_w_bitmap.bmiColors[i].rgbGreen = (_table_ptr[i] >> 16) & 0x0ff;
tmp_w_bitmap.bmiColors[i].rgbBlue = (_table_ptr[i] >> 24) & 0x0ff;
}
return CreateDIBitmap(GetDC(APP_hWnd), &tmp_w_bitmap.bmiHeader, CBM_INIT, _index_ptr, (BITMAPINFO*)&tmp_w_bitmap, DIB_RGB_COLORS);
}
WM_HSCROLL 訊息中使用了上述函式:
case WM_HSCROLL:
{
LRESULT slider_pos = SendMessage(GetDlgItem(_hwnd, IDS_DC_SLIDER), TBM_GETPOS, 0, 0);
wchar_t output[4];
wsprintf(output, L"%ld", slider_pos);
SetWindowText(GetDlgItem(_hwnd, IDS_DC_INTENSITY), output);
int intensity = _wtoi(output);
int intensity_offset = intensity * 256;
convert_hbm_texture_256 = APP_Make_HBITMAP_From_Bitmap_Indexed(256, 256, convert_table intensity_offset, convert_index);
RedrawWindow(_hwnd, NULL, NULL, RDW_INVALIDATE);
}
break;
在 WM_PAINT: 訊息中,我正在顯示位圖并實作它
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(_hwnd, &ps);
EndPaint(_hwnd, &ps);
// display 256 preview
PAINTSTRUCT ps_texture_256;
HDC hdc_texture_256 = BeginPaint(GetDlgItem(_hwnd, IDS_DC_TEXTURE_256), &ps_texture_256);
HDC hdc_tmp_256 = CreateCompatibleDC(hdc_texture_256);
SelectObject(hdc_tmp_256, convert_hbm_texture_256);
BitBlt(hdc_texture_256, 0, 0, 256, 256, hdc_tmp_256, 0, 0, SRCCOPY);
DeleteDC(hdc_tmp_256);
DeleteObject(convert_hbm_texture_256);
EndPaint(GetDlgItem(_hwnd, IDS_DC_TEXTURE_256), &ps_texture_256);
return (INT_PTR)TRUE;
}
解釋:
我正在使用滑塊在具有不同強度的顏色表中移動。每次我移動滑塊時,我的應用程式的記憶體使用量都會增加 - 我不知道為什么,因為我正在釋放 DeleteObject(convert_hbm_texture_256); 每次顯示后。
請注意,我也在我的應用程式的不同部分使用相同的函式 APP_Make_HBITMAP_From_Bitmap_Indexed,它作業正常并且沒有記憶體問題。但在這種情況下,我只使用一次(拖放后)。
提前致謝..
uj5u.com熱心網友回復:
您肯定在這里有設備背景關系的 GDI 泄漏APP_Make_HBITMAP_From_Bitmap_Indexed:
return CreateDIBitmap(GetDC(APP_hWnd), &tmp_w_bitmap.bmiHeader, CBM_INIT, _index_ptr, (BITMAPINFO*)&tmp_w_bitmap, DIB_RGB_COLORS);
你打電話GetDC,但從不打電話ReleaseDC。使用GDIView或Deleaker等 GDI 泄漏檢測工具檢查整個應用程式是個好主意。
WM_PAINT 處理程式應該處理呼叫其視窗程序的視窗。這樣的處理程式必須以單個 BeginPaint 開始并以單個 EndPaint 結束。
您的處理程式處理兩個視窗。
SelectObject 回傳舊物件的句柄。您必須稍后再選擇它。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/449961.html
