Results 1 to 3 of 3

Thread: Why does this leak memory? (pls help)

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516

    Unhappy Why does this leak memory? (pls help)

    As far as I can see, there is no way this can cause a memory leak, but it does. Doing it about twice takes 1% of GDI memory away (according to the 'Resource Meter). And since this will be executed a couple of dozen times at least, then it's important that there is no leakage. Help.

    Code:
    '// In a class module if that makes any difference
    
    Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
    
    Private Declare Function CreateSolidBrush Lib "gdi32" (ByVal crColor As Long) As Long
    
    Private Declare Function FillRect Lib "user32" (ByVal hdc As Long, lpRect As RECT, ByVal hBrush As Long) As Long
    
    Private Declare Function CreateFont Lib "gdi32" Alias "CreateFontA" (ByVal H As Long, ByVal W As Long, ByVal E As Long, ByVal O As Long, ByVal W As Long, ByVal i As Long, ByVal u As Long, ByVal S As Long, ByVal C As Long, ByVal OP As Long, ByVal CP As Long, ByVal Q As Long, ByVal PAF As Long, ByVal F As String) As Long
    
    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
    
    Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
    
    Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    
    Private Const DT_CENTER = &H1
    Private Const DT_VCENTER = &H4
    Private Const DT_SINGLELINE = &H20
    Private Const DT_NOPREFIX = &H800
    
    Private Const WM_SETFONT = &H30
    Private Const WM_GETFONT = &H31
    
    Private m_ThisRect As RECT
    
    Private Sub PageOut(Page As Long, Total As Long)
    Dim txtRect As RECT
    Dim hBr As Long, hFont As Long
    
    With txtRect
        .Top = m_ThisRect.Top - 35
        .Bottom = .Top + 30
        .left = m_ThisRect.left + ((m_ThisRect.Right - m_ThisRect.left) \ 3) + 15
        .Right = m_ThisRect.left + (((m_ThisRect.Right - m_ThisRect.left) \ 3) * 2) - 15
    End With
    
    hBr = CreateSolidBrush(GetSysColor(15))
    Call FillRect(frmMain.hdc, txtRect, hBr)
    Call DeleteObject(hBr)
    
    '// Leak occurs here
        hFont = CreateFont(18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Book Antiqua")
        Call SendMessage(frmMain.hwnd, WM_SETFONT, hFont, 0)
        Call DrawText(frmMain.hdc, "Page " & Page & " of " & Total, Len("Page " & Page & " of " & Total), txtRect, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE Or DT_NOPREFIX)
        Call DeleteObject(hFont)
    '//
    
    End Sub
    Why? Please help,

    me.
    Courgettes.

  2. #2
    Member
    Join Date
    Oct 2000
    Posts
    34
    I'm not positive about this, but I think the leak may be because you are setting the form's font via the WM_SETFONT message and then deleting it without restoring the original font first.

    I would try this instead:
    VB Code:
    1. '// Leak occurs here
    2. hFont = CreateFont(18, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, "Book Antiqua")
    3.  
    4. ' Get rid of this line, we don't need it
    5. 'Call SendMessage(frmMain.hwnd, WM_SETFONT, hFont, 0)
    6.  
    7. ' Select the new font into the DC
    8. hFontOld = SelectObject(frmMain.hdc, hFont)
    9. Call DrawText(frmMain.hdc, "Page " & Page & " of " & Total, Len("Page " & Page & " of " & Total), txtRect, DT_CENTER Or DT_VCENTER Or DT_SINGLELINE Or DT_NOPREFIX)
    10. ' Restore the original font into the DC before deleting the one we created
    11. Call SelectObject(frmMain.hdc, hFontOld)
    12.  
    13. ' Now we should be able to delete the font without worrying about any leaks :)
    14. Call DeleteObject(hFont)
    15. '//

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    I haven't tried it, but I think that would work.

    I had a similar leak involving CreatePen and SelectObject got rid of it, so that code has brought the memories flooding back.

    (leak, memories, flooding, ooh I'm going well, aren't I?)

    Thanks very much for answering my problem and it's probably the same sort of problem involved in a leak to do with CreateSolidBrush, so thanks double.

    A very grateful

    Me.
    Courgettes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width