Results 1 to 4 of 4

Thread: [RESOLVED] GlobalAlloc without GlobalFree?

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Resolved [RESOLVED] GlobalAlloc without GlobalFree?

    Hi,

    I'm in search of a memory leak in my application.

    I came across the following code (from the Common Controls Replacement Library):
    Code:
    Public Function PictureFromByteStream(ByRef ByteStream As Variant) As IPictureDisp
    Const GMEM_MOVEABLE As Long = &H2
    Dim IID As CLSID, Stream As IUnknown, NewPicture As IPicture
    Dim B() As Byte, ByteCount As Long
    Dim hMem As Long, lpMem As Long
    With IID
    .Data1 = &H7BF80980
    .Data2 = &HBF32
    .Data3 = &H101A
    .Data4(0) = &H8B
    .Data4(1) = &HBB
    .Data4(3) = &HAA
    .Data4(5) = &H30
    .Data4(6) = &HC
    .Data4(7) = &HAB
    End With
    If VarType(ByteStream) = (vbArray + vbByte) Then
        B() = ByteStream
        ByteCount = (UBound(B()) - LBound(B())) + 1
        hMem = GlobalAlloc(GMEM_MOVEABLE, ByteCount)
        If hMem <> 0 Then
            lpMem = GlobalLock(hMem)
            If lpMem <> 0 Then
                CopyMemory ByVal lpMem, B(LBound(B())), ByteCount
                GlobalUnlock hMem
                If CreateStreamOnHGlobal(hMem, 1, Stream) = 0 Then
                    If OleLoadPicture(Stream, ByteCount, 0, IID, NewPicture) = 0 Then Set PictureFromByteStream = NewPicture
                End If
            End If
        End If
    End If
    End Function
    It seems to me that the memory, allocated via GlobalAlloc, is not freed by GlobalFree (opposed to what should be done as stated in the documentation of GlobalAlloc).

    I'm not very familiar with the memory api's, so I'm wondering whether this is correct or not.

  2. #2
    Hyperactive Member
    Join Date
    Jan 2018
    Posts
    268

    Re: GlobalAlloc without GlobalFree?

    Do you have some reason to think there is a leak in this function, besides seeing an Alloc without a Free?

    It might be missing some error handling, but the second parameter of CreateStreamOnHGlobal is called fDeleteOnRelease. Since that is set then I presume the free is automatic.

  3. #3

    Thread Starter
    New Member
    Join Date
    Mar 2018
    Posts
    5

    Re: GlobalAlloc without GlobalFree?

    Quote Originally Posted by ahenry View Post
    Do you have some reason to think there is a leak in this function, besides seeing an Alloc without a Free?

    It might be missing some error handling, but the second parameter of CreateStreamOnHGlobal is called fDeleteOnRelease. Since that is set then I presume the free is automatic.
    Ah, that seems to be it! Overlooked that one. Thank you!

  4. #4
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    380

    Re: [RESOLVED] GlobalAlloc without GlobalFree?

    Quote Originally Posted by MBOS View Post
    I came across the following code (from the Common Controls Replacement Library):
    Here's a simpler alternative that frees you of deallocation worries:

    Quote Originally Posted by Bonnie West View Post
    BTW, here's an alternate version of the ArrayToPicture() function above that uses a different API that Microsoft claims has "better performance" than the CreateStreamOnHGlobal function:

    Code:
    Private Declare Function OleLoadPicture Lib "oleaut32.dll" (ByVal lpstream As IUnknown, ByVal lSize As Long, ByVal fRunmode As Long, ByRef riid As Any, ByRef lplpvObj As IPicture) As Long
    Private Declare Function SHCreateMemStream Lib "shlwapi.dll" Alias "#12" (ByRef pInit As Byte, ByVal cbInit As Long) As IUnknown
    
    'This function creates a StdPicture object from the passed *non-empty* Byte array
    
    Private Function ArrayToPicture(ByRef Data() As Byte) As IPicture
        Dim lSize As Long, IID_IPicture(0 To 1) As Currency, oStream As IUnknown
    
        lSize = UBound(Data) - LBound(Data) + 1&
        Set oStream = SHCreateMemStream(Data(0&), lSize)
    
        If Not oStream Is Nothing Then
            IID_IPicture(0&) = 116045007755044.6976@    '{7BF80980-BF32-101A-8BBB-00AA00300CAB}
            IID_IPicture(1&) = -612146501409303.8709@
            lSize = OleLoadPicture(oStream, lSize, 0&, IID_IPicture(0&), ArrayToPicture): Debug.Assert lSize = 0& 'S_OK
        End If
    End Function

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