Question about HDC , DC and how to correctly clean them up?

Did i cleaned up after myself in this code, i was getting a lot of memory usage and my malware executable is acting up after i run this code.

ps. Most of my code is from winprog.org

Also do i have to clean up after i use CreateCompatibleBitmap() function is so then how?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
case WM_TIMER:
            {

                RECT rcClient;

                HDC hdc = GetDC(hwnd);

                GetClientRect(hwnd, &rcClient);

                HDC hdcBuffer = CreateCompatibleDC(hdc);
                HBITMAP hbmBuffer = CreateCompatibleBitmap(hdc, (&rcClient)->right, (&rcClient)->bottom);
                HBITMAP hbmOldBuffer = (HBITMAP)SelectObject(hdcBuffer, hbmBuffer);

                HDC hdcMem = CreateCompatibleDC(hdc);

                HBITMAP hbmOld =  CreateCompatibleBitmap(hdc, (&rcClient)->right, (&rcClient)->bottom);

                FillRect(hdcBuffer, (&rcClient), (HBRUSH)GetStockObject(WHITE_BRUSH));

                hbmOld = (HBITMAP)SelectObject(hdcMem, CreateBitmapMask((HBITMAP)green, RGB(0, 0, 0)));//CreateBitmapMask fuction i created not related to windows stuff

                for(int i = 0; i < 40; i++){ //drawing 40 green squares inside this loop
                    x = i * 16;;
                    y = 0;

                    BitBlt(hdcBuffer, x, y, 16, 16, hdcMem, 0, 0, SRCAND);
                    SelectObject(hdcMem, (HBITMAP)green);

                    BitBlt(hdcBuffer, x, y, 16, 16, hdcMem, 0, 0, SRCPAINT);
                }

                BitBlt(hdc, 0, 0, (&rcClient)->right, (&rcClient)->bottom, hdcBuffer, 0, 0, SRCCOPY);


                SelectObject(hdcMem, hbmOld);
                DeleteDC(hdcMem);

                SelectObject(hdcBuffer, hbmOldBuffer);
                DeleteDC(hdcBuffer);
                DeleteObject(hbmBuffer);

                ReleaseDC(hwnd, hdc);
            }
            break;


Thank you for reading.
Last edited on
I think you have to destroy the bitmap created by CreateBitmapMask.
Also check the return value of DeleteDC and DeleteObject if they actually delete it.
Topic archived. No new replies allowed.