Gerbrand
Gerbrand

Reputation: 5434

System.OutOfMemoryException: Out of memory (GDI)

After installing my program on a windows vista premium, I'm getting the following exception.

The view that must be shown contains following controls: 2 textboxes, 3 labels, a button and linkbutton.

System.OutOfMemoryException: Out of memory.
   at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
   at System.Windows.Forms.PaintEventArgs.get_Graphics()
   at System.Windows.Forms.Control.PaintException(PaintEventArgs e)
   at System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e, Int16 layer, Boolean disposeEventArgs)
   at System.Windows.Forms.Control.OnPrint(PaintEventArgs e)
   at System.Windows.Forms.Control.WmPrintClient(Message& m)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
   at System.Windows.Forms.ContainerControl.WndProc(Message& m)
   at System.Windows.Forms.Form.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

Somebody had the same issue? How to solve it?

Upvotes: 3

Views: 7884

Answers (4)

johnc
johnc

Reputation: 40223

This is not necessarily an OOM error, GDI has a habit of throwing Out Of Memory whenever it throws an exception.

To quote Microsoft "GDI+ likes to return OutOfMemoryExceptions in cases that have nothing to do with memory". A nasty little 'idiosyncracy'

See here for details

Upvotes: 4

Steve Dunn
Steve Dunn

Reputation: 21741

How often are you displaying this form? It could be a handle problem with window handles (or lack of).

I've worked on projects that contain dialogs with hundreds of controls and there were memory/handle issues as the dialogs were not Disposed after use.

To work around this, they originally stored the handles for every control on the form the first time the form was displayed and reused them every time after.

In my opinion, it easier and simpler to just destroy what you don't need as soon as you've finished.

Upvotes: 1

Quibblesome
Quibblesome

Reputation: 25409

Might it be possible that you only detected this on a Vista box because there is less free memory than on your Windows XP boxes? If the machines are roughly the same spec then I would guess the Vista box would have less memory free and therefore highlight issues with memory leaks more quickly.

The other possibility is that you are trying to render too much, as the call stack states there is a scrollable control, is it possible you are rendering a bunch of stuff that isn't actually visible?

Upvotes: 1

Martin Peck
Martin Peck

Reputation: 11544

Does your app make use of any custom controls or controls you've written yourself? Can you repro this problem with a very simple form?

This...

http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/4bc34266-edf9-430c-ad5a-c6e29392eb2d

... and this...

http://social.expression.microsoft.com/Forums/zh-CN/netfxbcl/thread/7c4d2e73-6e73-4f10-a614-13fd76b2f419

... appears to be a similar problems. However they generally talk about custom controls that are failing to dispose object (and, as such, leak GDI handles).

Is it possible that somewhere else in your app you're leaking handles?

Upvotes: 3

Related Questions