WinForms手柄太多

我的生产,我的应用程序造成了两个不同的例外:

System.ArgumentException: Invalid Parameter. at System.Drawing.Image.get_RawFormat() at System.Windows.Forms.ToolStripItem.set_Image(Image value) at Zeiss.IMT.IME.CmmUI.Tools.ToolControl.updateControls() 

还有另一个例外,我想有同样的原因:

 System.ComponentModel.Win32Exception: The operation completed successfully at System.Drawing.BufferedGraphicsContext.CreateCompatibleDIB(IntPtr hdc, IntPtr hpal, Int32 ulWidth, Int32 ulHeight, IntPtr& ppvBits) at System.Drawing.BufferedGraphicsContext.CreateBuffer(IntPtr src, Int32 offsetX, Int32 offsetY, Int32 width, Int32 height) at System.Drawing.BufferedGraphicsContext.AllocBuffer(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle) at System.Drawing.BufferedGraphicsContext.AllocBufferInTempManager(Graphics targetGraphics, IntPtr targetDC, Rectangle targetRectangle) at System.Drawing.BufferedGraphicsContext.Allocate(IntPtr targetDC, Rectangle targetRectangle) at System.Windows.Forms.Control.WmPaint(Message& m) at System.Windows.Forms.Control.WndProc(Message& m) at System.Windows.Forms.DataGridView.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) 

在这两个例外,Windows任务pipe理器显示了很多句柄(> 8500)。 所以窗口句柄限制已达到,应用程序无法分配更多的内存/句柄。
到目前为止,我明白这个问题。
在这种情况下,我通过System.Drawing大量使用GDI绘图。 所以我search了我的代码,没有发布graphics句柄 – 没有成功。

所以问题是:
我怎样才能find未发布(本机)处理

你可以使用内存配置文件工具。 比如这个: http : //www.red-gate.com/products/dotnet-development/ants-memory-profiler/

但是你处置所有一次性物品? 画笔等等..

你可以发布你的绘图代码,也许我们可以看到错误的东西。

也许有一些刷子,…没有处置。

检查你的代码,并确保你不再需要时释放所有的一次性物品。 将一个可丢弃的对象包装成使用语句。

如果真的有这么多的GDI句柄需要,你可以增加最大的http://msdn.microsoft.com/en-us/library/windows/desktop/ms724291(v=vs.85).aspx 。