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.