News:

Masm32 SDK description, downloads and other helpful links
Message to All Guests

Main Menu

WM_PAINT memory leak problem

Started by xoreaxeax, July 04, 2012, 05:58:32 AM

Previous topic - Next topic

xoreaxeax

Hello all, I think I have a memory leak problem which I isolated in my code:


.if uMsg==WM_PAINT
    invoke BeginPaint,hWnd,ADDR ps
    mov hdc,eax

    invoke CreateFont,FONT1H,FONT1W,0,0,400,0,0,0,OEM_CHARSET,OUT_DEFAULT_PRECIS,CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH,ADDR TYPEFACE   
    invoke SelectObject,hdc,eax
    mov hfont,eax
   
    RGB 0,0,0
    invoke SetBkColor,hdc,eax
    RGB 0,255,0
    invoke SetTextColor,hdc,eax
    RGB Rr,Gg,Bb
    invoke SetTextColor,hdc,eax

    mov edi,pWorldTable
    mov esi,[edi+4]         ;get ID# of current active area
    lea edi,[edi+8]         ;point to first WorldX,WorldY
    dec esi
    shl esi,4
    lea edi,[edi+esi+8]   
    mov esi,[edi]           ;set ptr to active mem block/area
       
    .if (PAINT_CHAR>0)       ;==idx# to mob's X,Y in LocalTable
        push PAINT_CHAR
        pop edi
        UnCoords [esi+(edi*8)]
        mov ebx,[esi+(edi*8)+4]             ;->Char/Mob Sheet
        invoke SetTextColor,hdc,[ebx+OFFSET_CHARCOLOR]
        invoke TextOut,hdc,MobX,MobY,ADDR [ebx+OFFSET_CHARSYMB],1
        mov PAINT_CHAR,0
    .endif

    invoke EndPaint,hWnd,ADDR ps


It seems to happen specifically when the TextOut function is called.  Any ideas?  Thanks for any help!


qWord

Quote from: xoreaxeax on July 04, 2012, 05:58:32 AM
Hello all, I think I have a memory leak problem which I isolated in my code:
you have a (GDI) handle leak  ;)
MREAL macros - when you need floating point arithmetic while assembling!

jj2007

"When you no longer need the font, call the DeleteObject function to delete it"

Ryan

http://msdn.microsoft.com/en-us/library/windows/desktop/dd183499%28v=vs.85%29.aspx

What jj said.

MichaelW

xor,

SelectObject is destroying the font handle in EAX before you copy it to hFont.

You should be saving the previous object returned by SelectObject and selecting it back into the paint DC before you reach EndPaint.

You should be deleting the font when you finish with it. And assuming that there are no variables in the font creation, I think it should be created once in your WM_CREATE or WM_INITDIALOG handler and deleted when you close the window or dialog.
Well Microsoft, here's another nice mess you've gotten us into.

jj2007

Quote from: MichaelW on July 04, 2012, 06:56:48 AM
xor,

SelectObject is destroying the font handle in EAX before you copy it to hFont.

Yep, I had overlooked that one - thanks Michael :t

Wrong:
    invoke SelectObject,hdc,eax
    mov hfont,eax

Right:
    mov hfont,eax
    invoke SelectObject,hdc,eax

dedndave

what Michael said...
create the font once in WM_CREATE - save the font handle as hFont or something similar
delete the font object once in WM_DESTROY (use DeleteObject,hFont)
then - in WM_PAINT
invoke BeginPaint,hWnd,ADDR ps
    mov hdc,eax

    invoke SelectObject,hdc,hFont
    mov hFontOriginal,eax                  ;save the original hFont

;draw stuff here

    invoke SelectObject,hdc,hFontOriginal  ;restore the original hFont into the DC
    invoke EndPaint,hWnd,ADDR ps

xoreaxeax

Wow, you guys are awesome, thanks for all the help!  :t