Out of Memory Exception

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



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.


.



Relevant Pages

  • Re: Out of Memory Exception
    ... Question is how large are the bitmaps? ... >I have an app that's dealing with a few hundred bitmap thumbnails. ... > situations where painting would happen outside of the control, where GDI ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: need a thumbnail browser without memory limitations
    ... It reports 887 bitmaps and 5119DIBs. ... You need to remember though that not all jpegs have embedded thumbnails, and even when they do they are usually something like 160 x 120 pixels, so to create a thumbnail of the size you require you will need to stretch the extracted 160 x 120 thumbnail, resulting in a serious loss of image quality in the 400 x 300 output. ... The limit comes because a picturebox can ... Then, of course, you need to tackle your "limited number of thumbnails" in memory problem. ...
    (microsoft.public.vb.general.discussion)
  • Re: need a thumbnail browser without memory limitations
    ... The limit comes because a picturebox can ... you do not actually need a PictureBox of such a size to display your thumbnails. ... Then, of course, you need to tackle your "limited number of thumbnails" in memory problem. ... Whilst Autoredraw PictureBoxes are ideal for many purposes, and whilst they allow you to easily deal with memory bitmaps using the simple native VB drawing functions, they are not so good when you need hundreds or thousands of them because each one needs resourcees over and above the actual memory required for the bitmap they hold. ...
    (microsoft.public.vb.general.discussion)
  • Re: "Named Resource" question
    ... LMDShapeButtons as glyphs and some are used in TImage components. ... the bitmaps was used up to 8 times within the app. ...
    (borland.public.delphi.language.objectpascal)
  • Re: Uniting two bitmaps
    ... bitmaps to be combined is a rather intrinsic process. ... a bitmap not its physical border? ... Once again I describe the situation to be solved in my app: ... There are two bitmaps the same height placed next to each other at the ...
    (microsoft.public.dotnet.framework.drawing)