The MASM Forum Archive 2004 to 2012

General Forums => The Campus => Topic started by: Cobra on December 14, 2006, 06:33:25 AM

Title: Please help
Post by: Cobra on December 14, 2006, 06:33:25 AM
This bit of code seems to have a leak in it somewhere and for the life of me I can't figure out where. I do know that it's this code that has the problem because I removed it and ran my program without it and there wasn't any problem. This is the WM_PAINT handler by the way. Can someone please take a look and see what's going on?


    invoke  BeginPaint, @hWnd, addr ps

   
        invoke  LoadPng, 3, addr pngSize    ; size is not needed
        mov     bDigits, eax
        invoke  PreMul, eax
        invoke  LoadPng, 2, addr pngSize    ; size is not needed
        mov     bMeters, eax
        invoke  PreMul, eax
        invoke  LoadPng, 1, addr pngSize    ; we need this one
        mov     bMeterBackground, eax
        invoke  PreMul, eax
   
        invoke  GetDC, NULL
        mov     hdcScreenDC, eax

        invoke  CreateCompatibleDC, hdcScreenDC
        mov     hdcMemDC, eax
   
        invoke  SelectObject, hdcMemDC, bMeterBackground
        mov     hOldBitmap, eax
   
        invoke  CreateCompatibleDC, hdcScreenDC
        mov     hdcWindowDC, eax
   
        invoke  CreateCanvas, hdcWindowDC, 600, 50
        mov     cWin, eax
   
        invoke  SelectObject, hdcWindowDC, eax
   
        invoke  BitBlt, hdcWindowDC, 0, 0, 600, 50, hdcMemDC, 0, 0, SRCCOPY
   
        invoke  SelectObject, hdcMemDC, bMeters

        push    esi
        push    edi
   
        xor     esi, esi
        mov     edi, 7
@@:
        .if     bMuted
            mov     ecx, 30
        .else
            mov     ecx, 10
        .endif
        mov     edx, dwCurrentVolume
        cmp     esi, edx
        sbb     eax, eax
        and     eax, -10
        add     eax, ecx
        invoke  BitBlt, hdcWindowDC, edi, 16, 10, 18, hdcMemDC, eax, 0, SRCCOPY
        add     esi, 4
        add     edi, 22
        cmp     esi, 100
        jl      @B
   
        invoke  SelectObject, hdcMemDC, bDigits
   
        mov     eax, dwCurrentVolume
   
        .if     eax == 100
            mov     DigitsIndex[0 ], 1
            mov     DigitsIndex[4 ], 0
            mov     DigitsIndex[8 ], 0
            mov     DigitsIndex[12], 10
        .elseif eax < 10
            xor     edx, edx
            mov     ecx, 10
            div     ecx
            mov     DigitsIndex[0 ], 11
            mov     DigitsIndex[4 ], 11
            mov     DigitsIndex[8 ], edx
            mov     DigitsIndex[12], 10
        .else
            xor     edx, edx
            mov     ecx, 10
            div     ecx
            mov     DigitsIndex[0 ], 11
            mov     DigitsIndex[4 ], eax
            mov     DigitsIndex[8 ], edx
            mov     DigitsIndex[12], 10
        .endif
        sub     edi, 4  ; backup 4 pixels from last position
   
        mov     eax, DigitsIndex[0]
        lea     eax, [eax+eax*4]
        shl     eax, 1
        invoke  BitBlt, hdcWindowDC, edi, 16, 10, 18, hdcMemDC, eax, 0, SRCCOPY
        add     edi, 10
   
        mov     eax, DigitsIndex[4]
        lea     eax, [eax+eax*4]
        shl     eax, 1
        invoke  BitBlt, hdcWindowDC, edi, 16, 10, 18, hdcMemDC, eax, 0, SRCCOPY
        add     edi, 10

        mov     eax, DigitsIndex[8]
        lea     eax, [eax+eax*4]
        shl     eax, 1
        invoke  BitBlt, hdcWindowDC, edi, 16, 10, 18, hdcMemDC, eax, 0, SRCCOPY
        add     edi, 10
       
        mov     eax, DigitsIndex[12]
        lea     eax, [eax+eax*4]
        shl     eax, 1
        invoke  BitBlt, hdcWindowDC, edi, 16, 10, 18, hdcMemDC, eax, 0, SRCCOPY
       
        pop     edi
        pop     esi
   
        invoke  SetWindowPos,@hWnd,HWND_TOP,0,0,600,50,SWP_NOMOVE ; or SWP_NOZORDER
        invoke  UpdateLayeredWindow,@hWnd,hdcScreenDC,NULL,addr pngSize,hdcWindowDC,addr ptZero,0,addr blender, ULW_ALPHA

    invoke  EndPaint, @hWnd, addr ps

    invoke  SelectObject, hdcMemDC, hOldBitmap
    invoke  DeleteDC, hdcMemDC
    invoke  ReleaseDC, @hWnd, hdcScreenDC
    invoke  DeleteObject, hOldBitmap
    invoke  DeleteObject, bMeterBackground
    invoke  DeleteObject, hdcWindowDC
    invoke  DeleteObject, bMeters
    invoke  DeleteObject, cWin

$$default:
    invoke  DefWindowProc, @hWnd, @uMsg, @wParam, @lParam
    ret
   
$$return0:   
    xor     eax, eax
    ret

OSD     endp



[attachment deleted by admin]
Title: Re: Please help
Post by: Cobra on December 14, 2006, 07:21:03 AM
I fixed it. I had to move the LoadPng & PreMul calls to the WM_CREATE section. No more GDI leaks. On another note, could someone suggest a way to get this piece of code to be/perform better?

        mov     eax, dwCurrentVolume
   
        .if     eax == 100
            mov     DigitsIndex[0 ], 1
            mov     DigitsIndex[4 ], 0
            mov     DigitsIndex[8 ], 0
            mov     DigitsIndex[12], 10
        .elseif eax < 10
            xor     edx, edx
            mov     ecx, 10
            div     ecx
            mov     DigitsIndex[0 ], 11
            mov     DigitsIndex[4 ], 11
            mov     DigitsIndex[8 ], edx
            mov     DigitsIndex[12], 10
        .else
            xor     edx, edx
            mov     ecx, 10
            div     ecx
            mov     DigitsIndex[0 ], 11
            mov     DigitsIndex[4 ], eax
            mov     DigitsIndex[8 ], edx
            mov     DigitsIndex[12], 10
        .endif
Title: Re: Please help
Post by: Grincheux on December 14, 2006, 08:26:09 AM
I am not sure that the second CreateCompatibleDC will give good results.
Generally we can only use ONE compatible DC.
Title: Re: Please help
Post by: raymond on December 15, 2006, 04:29:32 AM
Quote from: Grincheux on December 14, 2006, 08:26:09 AM
I am not sure that the second CreateCompatibleDC will give good results.
Generally we can only use ONE compatible DC.

You can definitely use more than ONE compatible DC. I've done it many times without any problem. The only limit is probably the number of handles your program could open at any one time.

Raymond