Memory exception in .net winform treeview

I have a treeview and based on the treeview elements, I have a listview on the right. SO almost UI is the look of Windows Explorer. So, now the problem I am facing is when I delete a large number of objects from the list going to the right, the tree on the left side becomes partially colored (a small part that I can tell). When I complete the CLR exclusion from the VS IDE, it points to the string sampletree.EndUpdate (); with an exception to the memory. When I add the next item in the list, everything becomes normal, I mean that the tree is completely painted Exception I get

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

Do you have any ideas why my tree drawing is completely painted with only part and whole colors of the modification? code snippet is shown below

  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 } } 

UPDATE I use Font this way

 case State.Modified: NodeFont = new Font(TreeView.Font, FontStyle.Bold); break; 

This causes problems.

+4
source share
2 answers

This type of failure is usually caused by a GDI resource leak. This very often happens due to forgetting to call the Dispose () method on any object of the System.Drawing class. Typically, the garbage collector cleans up after you, but, especially when you use ownerdraw, it may not work often enough to prevent you from getting into trouble. Windows pulls the plug into your program when you consume 10,000 GDI objects, the result is kaboom.

You can easily diagnose this from the task manager. View + Select Columns and mark descriptors, USER objects, and GDI objects. Observe these added columns for your process during its use. A steady rise in numbers predicts OOM Kaboom.

First, look at the DrawNode event handler, as it is often called. But this may be caused by another coloring code. Make sure you create drawing objects like Graphics, Pen, Brush, Font, etc. Use the using statement to ensure that they will be disposed of after use. The diagnostics that you get from the task manager tell you when you are ahead.

+2
source

I just got into the same problem. This seems to be happening on Windows systems later BeginUpdate() XP, and when I deleted the calls to BeginUpdate() and EndUpdate() , this does not happen.

So, as a workaround, I would say try removing the BeginUpdate() and EndUpdate() calls. This means that when updating your nodes, you may experience some kind of visual stutter, but on the positive side, it will not crash. This is certainly a clear victory.

I did not find anything on MSDN / Connect that solves this problem, and I don’t have the time right now to build a standalone test case, but I think this is a mistake when processing bulk updates in TreeViews in later versions of Windows.

+1
source

All Articles