tal
tal

Reputation: 281

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.

Upvotes: 3

Views: 753

Answers (2)

Syclone0044
Syclone0044

Reputation: 128

For future reference, a very useful free tool is NirSoft GDIView which displays GDI usage per process and tracks changes (handle leaks) while it runs. So you can perform operations in your app and keep checking GDIView until you see the counter increasing, and then repeat the operations until you pinpoint which one is causing the unwarranted handle increase.

Upvotes: 0

tal
tal

Reputation: 281

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.

Upvotes: 4

Related Questions