Results 1 to 7 of 7

Thread: [RESOLVED] Memory leak GdipCreateBitmapFromScan0

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    286

    Resolved [RESOLVED] Memory leak GdipCreateBitmapFromScan0

    hello does anyone know why GdipCreateBitmapFromScan0 causes memory leak, next a very simple example with a timer calling the function and removing the image.

    In the task manager you can see how VB6.exe gradually increases its memory
    Code:
    Option Explicit
    Private Declare Sub GdiplusShutdown Lib "gdiplus" (ByVal Token As Long)
    Private Declare Function GdiplusStartup Lib "gdiplus" (Token As Long, inputbuf As GdiplusStartupInput, Optional ByVal outputbuf As Long = 0) As Long
    Private Declare Function GdipDisposeImage Lib "gdiplus" (ByVal Image As Long) As Long
    Private Declare Function GdipCreateBitmapFromScan0 Lib "GdiPlus.dll" (ByVal Width As Long, ByVal Height As Long, ByVal stride As Long, ByVal PixelFormat As Long, scan0 As Any, BITMAP As Long) As Long
    
    Private Type GdiplusStartupInput
        GdiplusVersion              As Long
        DebugEventCallback          As Long
        SuppressBackgroundThread    As Long
        SuppressExternalCodecs      As Long
    End Type
    Private Const PixelFormat32bppPARGB     As Long = &HE200B
    Private Const GDIP_OK As Long = 0&
    Dim GdipToken As Long
    
    Private Sub Form_Load()
        Dim GdipStartupInput As GdiplusStartupInput
        GdipStartupInput.GdiplusVersion = 1&
        Call GdiplusStartup(GdipToken, GdipStartupInput, ByVal 0&)
        Timer1.Interval = 100
    End Sub
    
    Private Sub Timer1_Timer()
        Dim hImage As Long
        If GdipCreateBitmapFromScan0(16&, 16&, 0&, PixelFormat32bppPARGB, ByVal 0&, hImage) = GDIP_OK Then
            Debug.Print hImage
            GdipDisposeImage hImage
        End If
    End Sub
    
    Private Sub Form_Unload(Cancel As Integer)
        Call GdiplusShutdown(GdipToken)
    End Sub
    leandroascierto.com Visual Basic 6 projects

  2. #2

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    286

    Re: Memory leak GdipCreateBitmapFromScan0

    Well, it seems, according to the example, the first minutes the memory is increasing but after a few minutes it stabilizes
    leandroascierto.com Visual Basic 6 projects

  3. #3
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,707

    Re: Memory leak GdipCreateBitmapFromScan0

    Quote Originally Posted by LeandroA View Post
    Well, it seems, according to the example, the first minutes the memory is increasing but after a few minutes it stabilizes
    It can stabilize even faster if instead of on a timer the image allocation/free is done repeatedly in an infinite loop, optionally with a single DoEvents to make UI responsive.

    cheers,
    </wqw>

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Dec 2008
    Location
    Argentina
    Posts
    286

    Re: Memory leak GdipCreateBitmapFromScan0

    Quote Originally Posted by wqweto View Post
    It can stabilize even faster if instead of on a timer the image allocation/free is done repeatedly in an infinite loop, optionally with a single DoEvents to make UI responsive.

    cheers,
    </wqw>
    If it is just an example to show that calling GdipCreateBitmapFromScan0 leaves some kind of memory leaks, but it reassures me to know that at some point it stops increasing and stabilizes
    leandroascierto.com Visual Basic 6 projects

  5. #5
    New Member
    Join Date
    Apr 2020
    Posts
    12

    Re: [RESOLVED] Memory leak GdipCreateBitmapFromScan0

    Do the results change if you pass valid parameters? The .NET equivalent constructor Bitmap::Bitmap(INT,INT,INT,PixelFormat,BYTE*) requires both a valid stride and a non-null pointer, neither of which is supplied in the sample code above.

  6. #6
    PowerPoster wqweto's Avatar
    Join Date
    May 2011
    Posts
    2,707

    Re: [RESOLVED] Memory leak GdipCreateBitmapFromScan0

    Quote Originally Posted by [citation-needed] View Post
    Do the results change if you pass valid parameters? The .NET equivalent constructor Bitmap::Bitmap(INT,INT,INT,PixelFormat,BYTE*) requires both a valid stride and a non-null pointer, neither of which is supplied in the sample code above.
    Passing both stride = 0 and scan0 = NULL is supported use-case for creating empty images. This is what the oridinary Bitmap::Bitmap (int width, int height) constructor uses to create a blank bitmap.

    Check out this line as implemented in libgdiplus by mono


    If you follow their GdipCreateBitmapFromScan0 they bail out early only on

    if (scan0 && (stride == 0 || stride % 4))
    return InvalidParameter;


    . . . but passing both scan0 and stridy empty is handled.

    cheers,
    </wqw>

  7. #7
    New Member
    Join Date
    Apr 2020
    Posts
    12

    Re: [RESOLVED] Memory leak GdipCreateBitmapFromScan0

    Excellent information, @wqweto. Thank you for correcting my wrong information!

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