GDI泄漏问题

Rom*_*n M 2 c winapi gdi

我注意到使用任务管理器,以下代码中有GDI泄漏.执行此代码的进程中GDI对象的计数每次执行时增加1,但我似乎无法找到问题.

任何帮助,将不胜感激.

// create new DC based on current    
HDC hDC = CreateCompatibleDC(GetDC());
// select a bitmap into the new DC and keep the old one
HGDIOBJ hOldObj = SelectObject (hDC,hBM);
// do somthing here --> 100% no leak here
SomeFunction (hDC);
// select the old object back from whence it came and delete whats returned   
DeleteObject (SelectObject (hDC,hOldObj));
// delete the DC
DeleteDC(hDC);
// delete the tmp object
DeleteObject (hOldObj);
Run Code Online (Sandbox Code Playgroud)

R M

Ism*_*ael 5

从评论中复制,我没有把它作为答案,因为我无法测试它,我不确定它是否正确,请测试它.

一般来说,嵌套调用即不是一个好主意

HDC hDC1 = GetDC(); 
HDC hDC2 = CreateCompatibleDC(hDC1); 
.. 
Run Code Online (Sandbox Code Playgroud)

代替

HDC hDC = CreateCompatibleDC(GetDC()); 
Run Code Online (Sandbox Code Playgroud)

(在你的代码中BTW没有发布GetDC返回的HDC.)