0

I've needed a function that would scan my Array and return ColumnWidths for my ListBox in UserForm. After a while, I've found This StackOverflow topic. There was a little mistake that I've fixed and used the code. Works really well!

But I've noticed that when I open and close my UserForm few times (10-20x), the opening is longer and longer. And the Memory usage of the Excel is getting bigger too. Around 1MB for each run.

So I think it is this module. Does anyone has the ability to see if there is a memory leak there?

The code I use is:

Option Explicit

' ==========================================================================
' SOURCE for this module, slightly modified code from TravelinGuy
' https://stackoverflow.com/questions/5012465/vb-macro-string-width-in-pixel
'
' Adds functions:
'     GetStringPixelHeight(text:Str, fontName:Str, fontSize:Single, _
'                          [isBold:Bool=False], [isItalics:Bool=False])
'     GetStringPixelWidth(text:Str, fontName:Str, fontSize:Single, _
'                         [isBold:Bool=False], [isItalics:Bool=False]))
'     GetLabelPixelHeight(label:Str)
'     GetLabelPixelWidth(label:Str)
' ==========================================================================

'API Declares

Private Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateFontIndirect Lib "gdi32.dll" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32.dll" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As FNTSIZE) As Long
Private Declare Function MulDiv Lib "kernel32.dll" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long

Private Const LOGPIXELSY As Long = 90

Private Type LOGFONT
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As Byte
    lfUnderline As Byte
    lfStrikeOut As Byte
    lfCharSet As Byte
    lfOutPrecision As Byte
    lfClipPrecision As Byte
    lfQuality As Byte
    lfPitchAndFamily As Byte
    lfFaceName As String * 32
End Type

Private Type FNTSIZE
    cx As Long
    cy As Long
End Type


Public Function GetLabelPixelWidth(label As String) As Integer

    Dim font As New StdFont
    Dim sz As FNTSIZE
    font.Name = "Arial Narrow"
    font.size = 9.5

    sz = GetLabelSize(label, font)
    GetLabelPixelWidth = sz.cx

End Function


Public Function GetStringPixelHeight(text As String, fontName As String, fontSize As Single, Optional isBold As Boolean = False, Optional isItalics As Boolean = False) As Integer

    Dim font As New StdFont
    Dim sz As FNTSIZE
    font.Name = fontName
    font.size = fontSize
    font.Bold = isBold
    font.Italic = isItalics

    sz = GetLabelSize(text, font)
    GetStringPixelHeight = sz.cy

End Function


Public Function GetStringPixelWidth(text As String, fontName As String, fontSize As Single, Optional isBold As Boolean = False, Optional isItalics As Boolean = False) As Integer

    Dim font As New StdFont
    Dim sz As FNTSIZE
    font.Name = fontName
    font.size = fontSize
    font.Bold = isBold
    font.Italic = isItalics

    sz = GetLabelSize(text, font)
    GetStringPixelWidth = sz.cx

End Function


Private Function GetLabelSize(text As String, font As StdFont) As FNTSIZE
    Dim tempDC As Long
    Dim tempBMP As Long
    Dim f As Long
    Dim lf As LOGFONT
    Dim textSize As FNTSIZE

    ' Create a device context and a bitmap that can be used to store a
    ' temporary font object
    tempDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0)
    tempBMP = CreateCompatibleBitmap(tempDC, 1, 1)

    ' Assign the bitmap to the device context
    DeleteObject SelectObject(tempDC, tempBMP)

    ' Set up the LOGFONT structure and create the font
    lf.lfFaceName = font.Name & Chr$(0)
    lf.lfHeight = -MulDiv(font.size, GetDeviceCaps(GetDC(0), 90), 72) 'LOGPIXELSY
    lf.lfItalic = font.Italic
    lf.lfStrikeOut = font.Strikethrough
    lf.lfUnderline = font.Underline
    If font.Bold Then lf.lfWeight = 800 Else lf.lfWeight = 400
    f = CreateFontIndirect(lf)

    ' Assign the font to the device context
    DeleteObject SelectObject(tempDC, f)

    ' Measure the text, and return it into the textSize SIZE structure
    GetTextExtentPoint32 tempDC, text, Len(text), textSize

    ' Clean up (very important to avoid memory leaks!)
    DeleteObject f
    DeleteObject tempBMP
    DeleteDC tempDC
    ' Return the measurements
    GetLabelSize = textSize

End Function

And function that is used to construct the ListBox.ColumnWidths string is:

' Return string with column widths from entered myarray values for lstbox.ColumnWidths
' Example: "80;40;49;65;30;21;19;65"
'---------------------------------------------------------------------------------------
Public Function getLstColumnWidths(ByVal myarray As Variant, _
                                   Optional multiplier As Single = 1) As String

    Dim currentVal As Long
    Dim longestVal As Long

    ' Get Column Widths from the largest string in column
    getLstColumnWidths = ""

    Dim i As Long
    Dim j As Long
    For i = LBound(myarray, 1) To UBound(myarray, 1)
        longestVal = 0
        For j = LBound(myarray, 2) To UBound(myarray, 2)
            Dim strText As String
            strText = myarray(i, j)
            ' MEMORY LEAKS?
            currentVal = GetStringPixelWidth(strText, "Tahoma", 8  ' <-- USED HERE
            longestVal = IIf(currentVal > longestVal, currentVal, longestVal)
        Next j
        getLstColumnWidths = getLstColumnWidths & _
            CStr(Round(longestVal * multiplier, 1)) & ";"
    Next i
    getLstColumnWidths = Left(getLstColumnWidths, Len(getLstColumnWidths) - 1)
End Function
Community
  • 1
  • 1
  • Without me pouring through all of your code, I can tell you the most common cause of memory leaks in VBA is **improper handling of objects**. Is every object you're creating being properly cleared and closed when you're done with it? Also, make sure you always have `Option Explicit` at the top of **every** module (to make sure you're declaring and manipulating your objects and variables properlly), and no code like `on error resume next` which will ignore errors. – ashleedawg Mar 14 '18 at 19:15
  • Hi, did you at least read my introductory text? I describe there that the main part that is, I think, leaking, is grabbed from another else. And I don't understand the code, too complicated for me. I don't do `Private Declare Function` stuff nowhere in my own code for example. But the original author does have "Clean up (very important to avoid memory leaks)" comment so I thought it would be ok. But from the moment I started to use the `GetStringPixelWidth`, it just slows everything down from repeating run to run. – Son Goku ssj4 Mar 14 '18 at 20:37
  • And yes. I'm using `Option Explicit` everywhere and trying to end objects well and do other stuff and that's the reason, I hope, that my 15 Userforms, 15 Modules, 6 Classes VBA project had no problem till today, when I implemented this Module. (I'm not using on error resume next) – Son Goku ssj4 Mar 14 '18 at 20:50
  • Last note: I've just commented the line in my code that is calling the function `getLstColumnWidths` and what a surprise. The code is fast again. :-) Sadly, I'm not experienced enough to find the leak or the reason for the code to be so slow and getting progressively slower each run by myself. That's the reason I'm writing here. – Son Goku ssj4 Mar 14 '18 at 21:44
  • I know this post is a million years old. Is the problem using the pattern "as new"? – user40176 Nov 25 '21 at 20:07

0 Answers0