View Full Version : .NET memory encreasing (GetBitmapBits)

09-24-2009, 08:52 PM
I am creating an application for my Logitech G19 keyboard with a LCD screen.
First i created a bitmap where i put some information on (such as text) to put on my lcd screen.

Dim bmpNet As Bitmap
...etc...etc... put information, text...etc...

Then, i have to convert the bitmap to bytes/intptr as the following line says with GetBitmapBits:

GetBitmapBits(bmpNet.GetHbitmap, lcdBMPQVGAx32.pixels.Length, lcdBMPQVGAx32.pixels)

Function is declere on top of my source code as:
<DllImport("gdi32.dll", CharSet:=CharSet.Auto, SetLastError:=True, ExactSpelling:=True)> _
Public Shared Function GetBitmapBits(ByVal hbmp As IntPtr, ByVal cbBuffer As Integer, ByVal lpvBits As Byte()) As Integer
End Function

Now, every time GetBitmapBits invokes, the memory of my application will encrease with 200kB/300kB. I have a timer ticking, so the memory will encrease every, 2 a 3 seconds.

Can someone help me with this problem?

Thanks in advance.

Colin Raaijmakers

09-24-2009, 10:49 PM
Well you need to make sure you delete the pointer to the bitmap after you are done with it. So if your timer generates a new bitmap and assigns the new pointer over the old one, you need to take the old pointer and delete it.

That is most likely why you are leaking memory.

However also be aware that you shouldn't be using that GDI function, it is only there for backwards compatibility with 16-bit applications. According to the MSDN this function is the one you should be using: http://msdn.microsoft.com/en-us/library/dd144879%28VS.85%29.aspx