我有一个 TreeView ,基于 TreeView 的项目,我在右侧有 ListView 。几乎 UI 就是我们的 Windows Explorer 外观。所以现在我面临的问题是当我从右侧的 ListView 中删除大量对象时,左侧的 TreeView 部分绘制(我可以说是一小部分)。当我从 VS IDE 附加 CLR excpetion 时,它指向行 sampletree.EndUpdate();内存不足除外。当我在 ListView 中添加下一个项目时,一切正常,我的意思是 TreeView 已完全绘制 我得到的异常是
System.OutOfMemoryException occurred
Message=Out of memory.
Source=System.Drawing
StackTrace:
at System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
at System.Drawing.Font.ToLogFont(Object logFont)
at System.Drawing.Font.ToHfont()
at System.Windows.Forms.Control.FontHandleWrapper..ctor(Font font)
at System.Windows.Forms.OwnerDrawPropertyBag.get_FontHandle()
at System.Windows.Forms.TreeView.CustomDraw(Message& m)
at System.Windows.Forms.TreeView.WmNotify(Message& m)
at System.Windows.Forms.TreeView.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.Control.SendMessage(Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.Control.ReflectMessageInternal(IntPtr hWnd, Message& m)
at System.Windows.Forms.Control.WmNotify(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
at System.Windows.Forms.UserControl.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr wndProc, IntPtr hWnd, Int32 msg, IntPtr wParam, IntPtr lParam)
at System.Windows.Forms.NativeWindow.DefWndProc(Message& m)
at System.Windows.Forms.Control.DefWndProc(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.TreeView.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.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
at System.Windows.Forms.UnsafeNativeMethods.SendMessage(HandleRef hWnd, Int32 msg, Int32 wParam, Int32 lParam)
at System.Windows.Forms.Control.EndUpdateInternal(Boolean invalidate)
at System.Windows.Forms.TreeView.EndUpdate()
你知道为什么我的 treeview 只绘制了一小部分而连续的修改绘制了全部吗?代码片段如下所示
if( ( values != null ) &&
( values .OverallState != ToBeDeleted ) &&
( values .OverallState != .Deleted ) )
{
TreeView tree = this.TreeView;
if( tree != null )
{
tree.BeginUpdate();
}
TryUpdate();
TryPopulate();
if( tree != null )
{
tree.EndUpdate(); // here exception coming
}
}
更新 我是这样使用字体的
case State.Modified:
NodeFont = new Font(TreeView.Font, FontStyle.Bold);
break;
这有什么问题吗
最佳答案
这种崩溃通常是由 GDI 资源泄漏引起的。这通常是由于忘记在任何 System.Drawing 类对象上调用 Dispose() 方法引起的。通常垃圾收集器会在您之后进行清理,但是,尤其是当您使用 ownerdraw 时,它可能不会经常运行以防止您遇到麻烦。当您消耗了 10,000 个 GDI 对象时,Windows 会拔掉您的程序的插头,这就是结果。
您可以从任务管理器轻松诊断此问题。查看 + 选择列并勾选句柄、用户对象和 GDI 对象。在您使用过程时观察那些为您的过程添加的列。稳步攀升的数字预示着 OOM Kaboom。
首先查看您的 DrawNode 事件处理程序,因为它可能会被频繁调用。但它也可能是由其他绘画代码引起的。确保使用 using 语句创建绘图对象,如 Graphics、Pen、Brush、Font 等,以便保证它们在使用后被释放。您从任务管理器获得的诊断会告诉您何时领先。
关于c# - .net winform treeview 内存不足异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16147635/