"Out of Memory" while populating TreeView - c#

I am facing "Out of Memory" issue when I am populating TreeView hierarchy using XML. Our XML structure is very complex and it is not in fix format. There are multiple level of child nodes. I am using recursion to iterate XML and populate TreeView structure. I tried to call GC.Collect. to clear memory but still it is throwing same error.
I am using C# of .NET framework 3.5 for development.
I will appreciate if you can help me to find solution for this.
I'm providing the Code, Which I'm using for populating the treeview, below
private void addTreeNode(XmlNode xmlNode, TreeNode treeNode)
{
string attribute = "";
treeView1.ImageList = imageList1;
treeViewResponse.ImageList = imageList1;
XmlNode xNode;
TreeNode tNode;
XmlNodeList xNodeList;
foo.MoveToFollowing(XPathNodeType.Element);
namespaces1 = foo.GetNamespacesInScope(XmlNamespaceScope.All);
if (xmlNode.HasChildNodes)
{
treeNode.ImageIndex = 0;
treeNode.SelectedImageIndex = 0;
xNodeList = xmlNode.ChildNodes;
for (int x = 0; x <= xNodeList.Count - 1; x++)
{
xNode = xmlNode.ChildNodes[x];
treeNode.Nodes.Add(new TreeNode(xNode.Name));
tNode = treeNode.Nodes[x];
//treeNode.Nodes[x].ImageIndex = -1;
addTreeNode(xNode, tNode);
}
}
else
{
treeNode.ImageIndex = 1;
treeNode.NodeFont = new Font("Arial", 8, FontStyle.Bold);
treeNode.SelectedImageIndex = 1;
treeNode.Text = xmlNode.OuterXml.Trim();
}
}

As Raymond suggested, you should construct the font one time and reuse it. I have noticed that even if you do this, changing node fonts immediately causes a redraw of the control which can cause TreeView to construct internal fonts in addition to the one you provided it. I have seen cases where this can cause the font handle usage to go up very fast such that the garbage collector does not free them fast enough. I think this is a bug in TreeView that is not very repeatable but will happen sometimes. A way to protect yourself against Treeview using all your application's GDI handles is to wrap a set of node adds or font changes in TreeView.BeginUpdate() and TreeView.EndUpdate() calls.
m_treeView.BeginUpdate();
try
{
// TreeNode adds changes here
}
finally
{
m_treeView.EndUpdate();
}
This will result in only one redraw even though you added or changed multiple nodes.
Steve

Related

Can I give controls an index property in C# like in VB?

I've found similar answers to my question before, but not quite to what I'm trying to do...
In Visual Basic (last I used it, in 06/07) there was an "Index" property you could assign to multiple controls with the same name. I used this primarily to loop through controls, i.e.:
For i = 1 to 500
picSeat(i).Print "Hello"
Next i
Is there a way to do this in C#? I know there is a .IndexOf(), but would that really help for what I'm doing? I want to have multiple controls with the same name, just different index.
This is a Windows Form Application, and I'm using Visual Studio 2012. I am talking about controls, not arrays/lists; this was possible in VB and I was wondering if it was possible at all in C#. So I want to have, say, 30 seats in a theatre. I want to have each seat represented by a picturebox named "picSeat". VB would let me name several objects the exact same, and would assign a value to a control property "Index". That way, I could use the above loop to print "Hello" in every picture box with only 3 lines of code.
No, this feature does not exist in C#, and was never implemented in the transition from classic VB to VB.Net.
What I normally do instead is put each of the controls in question in a common parent container. The Form itself can work, but if you need to distinguish these from others of the same type a GroupBox or Panel control will work, too. Then, you access the controls like this:
foreach (var picBox in parentControl.Controls.OfType<PictureBox>())
{
// do something with each picturebox
}
If you want to use a specific control, just write by name:
pictureBox6.SomeProperty = someValue;
If you need to change a specific control determined at run-time, normally this is in response to a user event:
void PictureBox_Click(object sender, EventArgs e)
{
var picBox = sender As PictureBox;
if (picBox == null) return;
//picBox is now whichever box was clicked
// (assuming you set all your pictureboxes to use this handler)
}
If you really really want the Control Arrays feature, you can do it by adding code to create the array to your form's Load event:
PictureBox[] pictureBoxes = Me.Controls.OfType<PictureBox>().ToArray();
Are we talking WinForms here? I'm not sure, but I don't think you can have multiple controls in winforms with same name. But I vaguely recall doing something similar and the solution was to name them Button_1, Button_2 etc. Then you can iterate through all controls and get your own index.
Beware though that if you want to instanciate a separate control for each seat in a theatre, you might run into some serious performance issues :) I've done something similar to that as well and ended up drawing the whole thing on a canvas and using mouse coordinates to handle the events correctly.
You may want to check out the Uid property of controls.
(http://msdn.microsoft.com/en-us/library/system.windows.uielement.uid(v=vs.110).aspx)
You can access Control through Uid property with the following
private static UIElement FindUid(this DependencyObject parent, string uid)
{
var count = VisualTreeHelper.GetChildrenCount(parent);
if (count == 0) return null;
for (int i = 0; i < count; i++)
{
var el = VisualTreeHelper.GetChild(parent, i) as UIElement;
if (el == null) continue;
if (el.Uid == uid) return el;
el = el.FindUid(uid);
if (el != null) return el;
}
return null;
}
And simply use
var control = FindUid("someUid");
I copied code from this post
If you create an indexed dictionary of your user control, it will behave pretty much the same as in VB6, though you'll not see it on the VS C# GUI. You'll have to get around the placement issues manually. Still - and most importantly -, you'll be able to refer to any instance by the index.
The following example is for 3 pieces for clarity, but of course you could automate every step of the process with appropriate loops.
public partial class Form1 : Form
{
...
Dictionary<int, UserControl1> NameOfUserControlInstance = new Dictionary<int, UserControl1>()
{
{ 1, new UserControl1 {}},
{ 2, new UserControl1 {}},
{ 3, new UserControl1 {}}
};
private void Form1_Load(object sender, EventArgs e)
{
NameOfUserControlInstance[1].Location = new System.Drawing.Point(0, 0);
NameOfUserControlInstance[2].Location = new System.Drawing.Point(200, 0);
NameOfUserControlInstance[3].Location = new System.Drawing.Point(400, 0);
Controls.Add(NameOfUserControlInstance[1]);
Controls.Add(NameOfUserControlInstance[2]);
Controls.Add(NameOfUserControlInstance[3]);
}
...
}
I like using Tags to apply any type of meta data about the controls
for (int i = 0; i< 10; ++i)
{
Button button = new Button();
button.Tag = i;
}

asp.net Treeview issue (can't get child nodes to display)

I have been working on this for a while, but cannot resolve the issue. I have searched S/O & Google, but no luck. Hoping someone on here can help resolve this.
I am not able to display the child nodes in my TreeView control. The data is being retrieved from a database.
The root node appears fine, but there are not child nodes displayed. How can I get the child nodes to be displayed?
My code is:
private void PopulateTreeNode(DataSet dsList)
{
var treeNode = new TreeNode();
foreach (DataRow dr in dsList.Tables[0].Rows)
{
if (dr["RecordTypeID"].ToString() == "1")
{
TreeNode NewNode = new TreeNode(dr["CustomerName"].ToString(), dr["customerID"].ToString());
treeCustomer.Nodes.Add(NewNode);
}
else if (dr["RecordTypeID"].ToString() == "2")
{
TreeNode pNode = new TreeNode(dr["CustomerName"].ToString(), dr["customerID"].ToString());
pNode.ChildNodes.Add(pNode);
}
else if (dr["RecordTypeID"].ToString() == "3")
{
TreeNode pNode = new TreeNode(dr["CustomerName"].ToString(), dr["customerID"].ToString());
pNode.ChildNodes.Add(pNode);
}
}
treeCustomer.Nodes.Add(treeNode);
treeCustomer.DataBind();
}
You don't need to call treeCustomer.DataBind() if you are manually adding nodes like this. It is probably clearing out your tree.
Have you tried calling treeCustomer.ExpandAll() ?
You may also need to set various properties of the treeview too; regarding displaying expand images etc..
It looks like you're adding pNode to the child nodes of pNode. This means it is never getting added to treeCustomer since only treeNode is being added to treeCustomer. It should probably be added to the child nodes of treeNode, or possibly added as a node of treeNode.

Tree View not showing update

I have a TreeView in my Windows Form user interface.
I want to fill it up from a database, but it does not refresh ever, even though if I WriteLine() every node, it is in memory as I expect.
In order to make it more easy to understand, I wrote a little example program that only has one button that creates a TreeView and a TreeView called treeView1 to display its content.
If anyone can tell me where I have misunderstood the use of the TreeView, it would be a tremendous help.
private void button1_Click(object sender, EventArgs e)
{
// create a tree
TreeView t = new TreeView();
TreeNode[] child = new TreeNode [1];
child[0]=new TreeNode("myCat");
child[0].Name = "IndependantOne";
TreeNode categoryNode = new TreeNode("catIdTag", child);
categoryNode.Name = "Citizen Cat 5239002147";
t.Nodes.Add(categoryNode);
// some stuff under the first node
TreeNode[] mouseNode = new TreeNode[1];
mouseNode[0] = new TreeNode("myMouse");
mouseNode[0].Name = "SqueakyOne";
TreeNode[] childItem = new TreeNode[1];
childItem[0] = new TreeNode("mouseIdTag", mouseNode);
childItem[0].Name = "Citizen Mouse 54655654649";
TreeNode eltNode = new TreeNode("Cheese", childItem);
eltNode.Name = "Emmental";
t.Nodes["Citizen Cat 5239002147"].Nodes.Add(eltNode);
// fill in the winform treeview
if (t != null)
{
//treeView1.Visible = false;
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
treeView1.TopNode = t.TopNode;
foreach (TreeNode n in t.Nodes)
{
treeView1.Nodes.Add(n);
System.Diagnostics.Debug.WriteLine("Category Node contains: " + treeView1.Nodes[n.Name].Name + " at " + treeView1.Nodes[n.Name].Level);
foreach (TreeNode no in treeView1.Nodes[n.Name].Nodes)
{
System.Diagnostics.Debug.WriteLine("Category Node Nodes contains: " + no.Name);
}
}
/*
This part I tried and it doesn't work either, still add it in the question if anyone knows if it's wiser?
this.treeView1 = t;
this.treeView1.Location = new System.Drawing.Point(233, 12);
this.treeView1.Name = "treeView1";
this.treeView1.Size = new System.Drawing.Size(351, 277);
this.treeView1.TabIndex = 11;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
*/
treeView1.EndUpdate();
this.treeView1.Update();
this.treeView1.Refresh();
treeView1.Show();
//this.Refresh();
}
}
I also tried setting the treeView1 with
treeView1 = t;
It was not a success...
change
treeView1.Nodes.Add(n);
with
treeView1.Nodes.Add((TreeNode)n.Clone());
You cannot host the same node in more than one TreeView control.
Alternatively, you can remove nodes from source TreeView before adding them to other TreeView:
while(t.Nodes.Count != 0)
{
var node = t.Nodes[0];
t.Nodes.RemoveAt(0);
treeView1.Nodes.Add(node);
}
And I hope that there is a real reason why you create and fill tree view in your method instead of directly filling an already existing one. If it is not an intended behavior, remove the if block completely and change TreeView t = new TreeView(); to var t = treeView1.
This behavior is expected because with this code you`re confusing node-to-treeview relationship nature of the native .NET treeview control. Instead when moving nodes between treeviews (t -> treeView1) you need to clone them as suggested. Without that moved node still linked with the old treeview (see node.Treeview property) and because original tree (t) is not visible/added to any parent (form), I guess node will be invisible as well.
Also, the way you`re working with data loading (through creating new treeview) is pretty bad pattern. Instead you need to download your data (async I guess) into a temp buffer and recreate the treeView1 at once when data will be available with BeginUpdate/EndUpdate calls.
PS. Replacing treeView1 variable with 't' won't work as well because you don't replace the treeview control instance in the parent form/panel Controls property with this code.

Garbage Collection of orphaned objects (tree-nodes) works for the "release-exe" but not in VS-debugger

Situation
According to this accepted answer, if the "GC 'sees' a cyclic reference of 2 or more objects which are not referenced by any other objects or permanent GC handles, those objects will be collected."
I wanted to know if garbage collection works for a super simple tree structure that doesn't even have content, just tree-nodes with parent- & child-references.
Imagine you create a root-node add a child to it and then a child to the child and so on, so not really a tree but more like a list (each node has at most one child and one parent).
If we then remove the root's child and all references to node's within this child's subtree as I understand the answer above, the garbage collector should clean up the subtree.
Problem Description
If you have a look at the Main-method in the test-code below, when running the exe from the Release-directory I get the behavior I would expect memory consumption increases upto ~1GB then goes down to ~27MB (after the 1. GC.collect) up again and then down to ~27MB again (for the 2. GC.collect).
Now when running it in the debugger memory consumption for doing this goes up to ~1GB and for the 1.GC.collect memory consumption stays exactly where it was then goes up to 1.6GB withing the 2nd for-loop takes ages there and then I finally get an OutOfMemoryException within the 2nd for-loop.
Questions
Why do I get this behavior in the debugger?
Shouldn't garbage collection work during debugging as well, am I missing some Info about the debugger?
Side notes
I'm using visual studio 2010 Express edition
I only call GC.Collect() for the specific testing purposes here to be sure that garbage collection should have taken place.(I don't plan to use it normally)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tree
{
class Program
{
static void Main(string[] args)
{
TreeNode root = new TreeNode(null); // the null-argument is the parent-node
TreeNode node = root;
for (int i = 0; i < 15000000; i++)
{
TreeNode child = new TreeNode(node);
node = child;
}
root.RemoveChild(root.Children[0] );
node = root;
GC.Collect();
for (int i = 0; i < 15000000; i++)
{
TreeNode child = new TreeNode(node);
node = child;
}
root.RemoveChild(root.Children[0]);
node = root;
GC.Collect();
Console.ReadLine();
}
}
}
I only include the following code in case you want to test it for yourself, it's not really useful
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Tree
{
class TreeNode
{
public TreeNode Parent { get; private set; }
public List<TreeNode> Children { get; set; }
public TreeNode(TreeNode parent)
{
// since we are creating a new node we need to create its List of children
Children = new List<TreeNode>();
Parent = parent;
if(parent != null) // the root node doesn't have a parent-node
parent.AddChild(this);
}
public TreeNode(TreeNode parent, List<TreeNode> children)
{
// since we are creating a new node we need to create its List of children
Children = new List<TreeNode>();
Parent = parent;
if (parent != null) // the root node doesn't have a parent-node
parent.AddChild(this);
Children = children;
}
public void AddChild(TreeNode child)
{
Children.Add(child);
}
public void RemoveChild(TreeNode child)
{
Children.Remove(child);
}
}
}
This is by design. The lifetime of an object reference in a method is extended to the end of the method when the debugger is attached. This is important to make debugging easy. Your TreeNode class keeps both a reference to its parent and its children. So any reference to a tree node keeps the entire tree referenced.
Including the child reference, it keeps the removed section of the tree referenced. While it is no longer in scope by the time you call GC.Collect(), it still exists in the method's stack frame. Scope is a language feature, not a runtime feature. Without a debugger, the jitter tells the garbage collector that the child reference is no longer live at the end of the for loop. So its referenced nodes can be collected.
Note that you won't get OOM when you set child explicitly to null:
for (int i = 0; i < 15000000; i++)
{
TreeNode child = new TreeNode(node);
node = child;
child = null;
}
Do not write that kind of code, you've made a very artificial example.

How to avoid winforms treeview icon changes when item selected

I'm experimenting with a treeview in a little C#/Winforms application. I have programatically assigned an ImageList to the treeview, and all nodes show their icons just fine, but when I click a node, its icon changes (to the very first image in the ImageList). How can I get the icon to remain unchanged?
BTW: The "SelectedImageIndex" is set to "(none)", since I don't really know what to set it to, since the image-index is different for the nodes (i guess?).
UPDATE: Here is the code of the application (I'm using Visual Studio Express 2008):
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
treeView1.BeginUpdate();
treeView1.Nodes.Clear();
treeView1.Nodes.Add("root","Project", 0);
treeView1.Nodes[0].Nodes.Add("Foo", "Foo", 2);
treeView1.Nodes[0].Nodes[0].Nodes.Add("Fizz", "Fizz", 3);
treeView1.Nodes[0].Nodes[0].Nodes.Add("Buzz", "Buzz", 3);
treeView1.Nodes[0].Nodes.Add("Bar", "Bar", 1);
treeView1.Nodes[0].Nodes[1].Nodes.Add("Fizz", "Fizz", 2);
treeView1.Nodes[0].Nodes[1].Nodes[0].Nodes.Add("Buzz", "Buzz", 3);
treeView1.EndUpdate();
treeView1.ImageList = imageList1;
}
}
}
Simply set the SelectedImageIndex for each node to the same value as ImageIndex. So, if you're creating your node programatically:
TreeNode node = new TreeNode("My Node");
node.ImageIndex = 1;
node.SelectedImageIndex = 1;
Or you can specify the whole lot in the constructor:
TreeNode node = new TreeNode("My Node", 1, 1);
You can do the same thing using the design time editor if you're adding nodes at design time. You just need to set the SelectedImageIndex at the node level and not at the TreeView level.
Hi You can also use the below code:
TreeNode Node = eventArgs.Node;
Node.SelectedImageKey = Node.ImageKey;
what can be done here is, we can utilize TreeView's HitTest method which gives the node information at a given point. Then with that info we can reset the Image to previous. Setting SelectedImageIndex to ImageIndex .Like so
var selectedNodeInfo = treeView.HitTest(treeView.PointToClient(Cursor.Position));
selectedNodeInfo.Node.SelectedImageIndex = selectedNodeInfo.Node.ImageIndex;

Categories