WinForms太多的句柄 [英] WinForms too many handles

查看:112
本文介绍了WinForms太多的句柄的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我们的生产中,我的应用程序导致了两个不同的异常:

I our production, my application caused two different exceptions:

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()

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

There is another exception that i suppose to have the same cause:

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任务管理器都会显示很多句柄(> 8500).因此达到了Windows句柄限制,并且应用程序无法分配更多的内存/句柄.
到目前为止,我了解问题所在.
在这种情况下,我通过System.Drawing大量使用GDI绘图.所以我搜索了没有释放图形句柄的代码-没有成功.

At both exceptions, the windows task manager shows up a lot of handles (>8500). So the windows handle limit is reached and the application fails to allocate more memory/handles.
So far i understand the problem.
In this case i'm heavily using GDI drawing via System.Drawing. So i searched my code where graphics handles are not released - without success.

所以问题是:
我如何找到未发布的(本机)句柄??

So the question is:
How can i find unreleased (native) handles???

推荐答案

您可以使用内存配置文件工具.例如以下示例: http://www.red-gate .com/products/dotnet-development/ants-memory-profiler/

You could use memory profile tools. For example this one: http://www.red-gate.com/products/dotnet-development/ants-memory-profiler/

但是您是否要处理所有一次性对象?刷子等.

But are you disposing all Disposable objects? Brushes and so on..

您可以发布您的绘图代码,也许我们可以看到错误的地方.

You could post your drawing code perhaps we can see something wrong.

也许有些刷子没有被丢弃.

Perhaps there are some brushes,... that aren't disposed.

这篇关于WinForms太多的句柄的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆