-
Jan 23rd, 2021, 09:20 AM
#1
Thread Starter
Hyperactive Member
[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
-
Jan 23rd, 2021, 12:10 PM
#2
Thread Starter
Hyperactive Member
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
-
Jan 23rd, 2021, 12:43 PM
#3
Re: Memory leak GdipCreateBitmapFromScan0
 Originally Posted by LeandroA
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>
-
Jan 23rd, 2021, 05:31 PM
#4
Thread Starter
Hyperactive Member
Re: Memory leak GdipCreateBitmapFromScan0
 Originally Posted by wqweto
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
-
Jan 26th, 2021, 08:57 AM
#5
New Member
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.
-
Jan 26th, 2021, 12:11 PM
#6
Re: [RESOLVED] Memory leak GdipCreateBitmapFromScan0
 Originally Posted by [citation-needed]
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>
-
Jan 27th, 2021, 08:44 AM
#7
New Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|