2013-09-25 43 views
0

所以,我对WinAPI非常陌生,并且我成功地加载了一个可以用箭头键移动的精灵。我的老师告诉我要对'内存泄漏'非常小心,我不确定如何以正确的方式释放内存,因为当我在进行控制台编程时,C++已经为我完成了。我运行该程序,并在将角色移动了一段时间之后冻结了我的电脑一分钟,所以我猜我已经做错了什么。你能告诉我如何正确地释放内存(如果我做错了),如果我的位图方法有什么不好,需要优化?谢谢!这是代码。C++ WinAPI位图内存泄漏?

void LoadAndBlitBitmap(LPCSTR szFileName, HWND hwnd, HDC winDC, int xPos, int yPos) 
{ 
//DEFINITIONS 

/*TransparentBlt(destDC, destXpos, destYpos, sizeX, sizeY, srcDC, 0, 0, sizeX, sizeY, RGB(a, b, c)) = TRANSPARENT BITMAP BLIT. Ex. RGB(255, 255, 255) if background is white*/ 
/*BitBlt(destDC, destXpos, destYpos, sizeX, sizeY, srcDC, 0, 0, SRCCOPY);       = SOLID BITMAP BLIT WITH NO TRANSPARENCY*/ 

//END DEFINITIONS 

//-----Get the size of the window--------- 
RECT rect; 
int win_width = 0; 
int win_height = 0; 
if(GetWindowRect(hwnd, &rect)) 
{ 
    win_width = rect.right - rect.left; //Subtracting the right coordinate with the left gives the width 
    win_height = rect.bottom - rect.top; //Subtracting the bottom coordinate with the top gives the height 
} 
//---------------------------------------- 

HDC  hdcMem = CreateCompatibleDC(winDC);        //Create the DC that will hold the off-screen printing. (Double Buffering "Anti-Flicker" Method) 
HBITMAP hbmMem = CreateCompatibleBitmap(winDC, win_width, win_height);  //Create the bitmap with the size of the window 
HANDLE hOld = SelectObject(hdcMem, hbmMem);        //Set the paintable bitmap to the off-screen DC 


//Draw to the off-screen DC 
HBITMAP bitmap = (HBITMAP)LoadImage(NULL, szFileName, IMAGE_BITMAP, 69, 69, LR_LOADFROMFILE);  //Load the .bmp from a file 
HDC blockDC = CreateCompatibleDC(NULL);                //Create a DC to hold the .bmp 
SelectObject(blockDC, bitmap);                  //Select the .bmp to the DC 
TransparentBlt(hdcMem, xPos, yPos, 69, 69, blockDC, 0, 0, 69, 69, RGB(255, 255, 255));    //Blit the .bmp to the DC*/ 


//Transfer off-screen DC to the screen 
BitBlt(winDC, 0, 0, win_width, win_height, hdcMem, 0, 0, SRCCOPY); 

// Uninitialize and deallocate resources 
SelectObject(hdcMem, hOld); 
DeleteDC(hdcMem); 
SelectObject(blockDC, hOld); 
DeleteDC(blockDC); 
DeleteObject(bitmap); 
} 

回答

2

两件事情是错误的:

SelectObject(blockDC, hOld); 

hOld没有从blockDC来,它来自hdcMem。您甚至没有保存blockDC的旧位图。更改为:

HBITMAP hOld2 = SelectObject(blockDC, bitmap); 
// and 
SelectObject(blockDC, hOld2); 

其次,您并未在任何地方删除hbmMem。在底部添加:

DeleteObject(hbmMem); 

实际上,第三件事情也是错误的 - 您不会检查您所做的任何API调用中的失败。你应该检查像CreateCompatibleDC这样的东西是否返回NULL,如果是,则中止并清除。

+0

糟糕,我忘了删除那个,为它添加了一个删除,冻结停止了。谢谢您的帮助! –

+0

@TobiasSundell很高兴我可以帮忙,随时接受答案:) –

+0

我有我找到的功能的备份。它包含了很多对失败的检查。我删除了它们,因为它们的代码非常大:p –