Serious memory leak, GDI, manual double buffer, bitblt

qmp

Member
Joined
May 23, 2005
Messages
14
Ok.. I have a picChart picturebox on my form that resizes with the form. It is double buffered by me so when the picChart chagnes size, I must resize my backbuffer...

I can do any of my drawing at any time using GDI+ and memDC. It works very well and my picChart is redrawn any time the user drags stuff over it. bitblt seems very fast as well although memDC.DrawLine does not seem as fast as GDI MoveToEx and LineTo.. but its very easy to implement.

When the user resizes the form, i get SEVERE memory leaks.. I am completely lost now. I cant figure out the solution.

I have tried using a static backbuffer size and instantiate it outside of the resize form but that still does not work. Im at a loss now.
Code:
private Graphics memDC;
IntPtr memdc;

private void picChart_Resize(object sender, EventArgs e)
{
	// If we have already created a dc for mem, lets free it up first
	if (memdc.ToInt32() != 0)
		DeleteDC(memdc);

	// Resize our backbuffer
	Bitmap memBmp = new Bitmap(picChart.Width, picChart.Height, System.Drawing.Imaging.PixelFormat.Format16bppRgb555 );
	Graphics clientDC = picChart.CreateGraphics();

	IntPtr hdc = clientDC.GetHdc();
	memdc = CreateCompatibleDC(hdc);
	SelectObject(memdc, memBmp.GetHbitmap());
	memDC = Graphics.FromHdc(memdc);

	// Clean up
	DeleteObject(memBmp.GetHbitmap());
	clientDC.ReleaseHdc(hdc);
	clientDC.Dispose();
	memBmp.Dispose();


	// Clear out our back buffer
	memDC.FillRectangle(new SolidBrush(Color.Black), 0, 0, picChart.Width, picChart.Height);
}

private void picChart_Paint(object sender, PaintEventArgs e)
{
	// Copy Region from memDC
	IntPtr hdc = e.Graphics.GetHdc();
	IntPtr hMemdc = memDC.GetHdc();

	BitBlt(hdc, e.ClipRectangle.X, e.ClipRectangle.Top, e.ClipRectangle.Width, e.ClipRectangle.Height, hMemdc, e.ClipRectangle.X, e.ClipRectangle.Y, 0x00CC0020);

	e.Graphics.ReleaseHdc(hdc);
	memDC.ReleaseHdc(hMemdc);
}
 
Back
Top