GDI leak problem

I noticed, using the task manager, that the following code has a GDI leak in it. The number of GDI objects in the process of executing this code increases by 1 each time it is executed, but I can not find the problem.

Any help would be appreciated.

// 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);

RM

+2
source share
7 answers

Copying from a comment, I did not answer this as I can’t check, and I was not sure if this is correct, please test it.

In general, it's nice to have nested calls, i.e.

HDC hDC1 = GetDC(); 
HDC hDC2 = CreateCompatibleDC(hDC1); 
.. 

instead

HDC hDC = CreateCompatibleDC(GetDC()); 

(By the way, in your code, the HDC returned by GetDC is not released.)

+5
source

Make sure you call ReleaseDCnot DeleteDCon the descriptors returned from GetDC.

+4

deleaker .

+1

( , , - xhantt)

, DCC, GetDC() , .

0

, . -, GDI.

MFC , GDI, CDC CMemoryDC .. , .

0

. GetDC() .

0

hOldObj

DeleteObject (hOldObj);

0

Source: https://habr.com/ru/post/1776216/


All Articles