Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GDI Leak Problem

I'm experiencing memory leaks while running the following GDI code:

HDC hdcScreen = GetDC(NULL);
HDC hdcMem = CreateCompatibleDC(hdcScreen); 
HBITMAP hbmpOld = (HBITMAP) SelectObject(hdcMem, hBmp); // apparently here is the leak 

// do something

SelectObject(hdcMem, hbmpOld); //placing the old object back. The return object is handled elseware
DeleteDC(hdcMem);  // after CreateCompatibleDC
ReleaseDC(NULL, hdcScreen); // after GetDC

I already looked at similar threads, such as this but I couldn't find the problem. Any help would be appreciated.

DeleteDC, ReleaseDC return value was checked to be true (no errors).

Thanks, Tal.

like image 272
tal Avatar asked Feb 25 '26 07:02

tal


1 Answers

Solved. The problem was hBmp wasn't correctly initialized, so there was a crash at the SelectObject - no error, just the function exited, skipping the "//do something" and the releases part.

like image 53
tal Avatar answered Feb 28 '26 04:02

tal