P
Pete Davis
I have an app that's dealing with a few hundred bitmap thumbnails.
I only instantiate the bitmaps that are visible at a given time and dispose
of them if they get scrolled out of view. I've used the .NET memory profiler
and I've verified there's no leak in my bitmaps, nor are there any other
apparent leaks. So, at a given time, there may be 30 or so bitmaps displayed
at a given time.
Still, somehow, I'm getting random errors in the painting that I can only
assume is coming from some sort of GDI resource issue. I've also had
situations where painting would happen outside of the control, where GDI was
clearly confused.
Here's the tail end of a stack dump from a recent OutOfMemory exception:
system.drawing.dll!System.Drawing.Graphics.FromHdcInternal(int hdc = 0) +
0x56 bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.CreateBuffer(int
src = 738299658, int offsetX = 0, int offsetY = 0, int width = 1333, int
height = 1086) + 0x1e4 bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.AllocBuffer(System.Drawing.Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing.Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x1a4 bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.AllocBufferInTempManager(System.Drawing.Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing.Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x3e bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.AllocBuffer(int
target = 738299658, System.Drawing.Rectangle targetBounds = {X=0 Y=0
Width=1333 Height=1086}) + 0x3f bytes
system.windows.forms.dll!System.Windows.Forms.Control.WmPaint(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x23a bytes
system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x2d6 bytes
The code that triggers this is actually an Application.DoEvents() call, so
it's simply the OnPaint being called, which is where the control paints the
thumbnails.
Any ideas on how I can diagnose this? I was sure .NET Memory Profiler would
show something leaking, but there's just nothing leaking that I can find..
The app is using about 70MB of ram, but it stays pretty consistently around
that figure through the lifetime of the app and is well within my 2.5GB of
physical ram.
I do make some P/Invoke GDI calls in the OnPaint. I have a method that draw
captions for each thumbnail and it has to call GetTextExtentPoint32 to
determine the text size. To do this, I basically do:
IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hdc, _labelFont.ToHfont());
SIZE textSize = new SIZE();
GetTextExtentPoint32(hdc, nameTxt, nameTxt.Length, ref textSize);
SelectObject(hdc, oldFont);
g.ReleaseHdc(hdc);
Am I doing something wrong here that's causing the HDC to leak?
Thanks for any ideas anyone can provide in tracking this down.
I only instantiate the bitmaps that are visible at a given time and dispose
of them if they get scrolled out of view. I've used the .NET memory profiler
and I've verified there's no leak in my bitmaps, nor are there any other
apparent leaks. So, at a given time, there may be 30 or so bitmaps displayed
at a given time.
Still, somehow, I'm getting random errors in the painting that I can only
assume is coming from some sort of GDI resource issue. I've also had
situations where painting would happen outside of the control, where GDI was
clearly confused.
Here's the tail end of a stack dump from a recent OutOfMemory exception:
system.drawing.dll!System.Drawing.Graphics.FromHdcInternal(int hdc = 0) +
0x56 bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.CreateBuffer(int
src = 738299658, int offsetX = 0, int offsetY = 0, int width = 1333, int
height = 1086) + 0x1e4 bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.AllocBuffer(System.Drawing.Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing.Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x1a4 bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.AllocBufferInTempManager(System.Drawing.Graphics
targetGraphics = <undefined value>, int targetDC = 738299658,
System.Drawing.Rectangle targetBounds = {X=0 Y=0 Width=1333 Height=1086}) +
0x3e bytes
system.windows.forms.dll!System.Windows.Forms.GraphicsBufferManager.DibGraphicsBufferManager.AllocBuffer(int
target = 738299658, System.Drawing.Rectangle targetBounds = {X=0 Y=0
Width=1333 Height=1086}) + 0x3f bytes
system.windows.forms.dll!System.Windows.Forms.Control.WmPaint(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x23a bytes
system.windows.forms.dll!System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message
m = {System.Windows.Forms.Message}) + 0x2d6 bytes
The code that triggers this is actually an Application.DoEvents() call, so
it's simply the OnPaint being called, which is where the control paints the
thumbnails.
Any ideas on how I can diagnose this? I was sure .NET Memory Profiler would
show something leaking, but there's just nothing leaking that I can find..
The app is using about 70MB of ram, but it stays pretty consistently around
that figure through the lifetime of the app and is well within my 2.5GB of
physical ram.
I do make some P/Invoke GDI calls in the OnPaint. I have a method that draw
captions for each thumbnail and it has to call GetTextExtentPoint32 to
determine the text size. To do this, I basically do:
IntPtr hdc = g.GetHdc();
IntPtr oldFont = SelectObject(hdc, _labelFont.ToHfont());
SIZE textSize = new SIZE();
GetTextExtentPoint32(hdc, nameTxt, nameTxt.Length, ref textSize);
SelectObject(hdc, oldFont);
g.ReleaseHdc(hdc);
Am I doing something wrong here that's causing the HDC to leak?
Thanks for any ideas anyone can provide in tracking this down.