'System.StackOverflowException: 'Exception_WasThrown' using TreeView
I am using VS2022(V17.1.6) 64bit and have a C# Winform with a TreeView control. This displays the hierarchical relationship between Product Categories and Products within each category - maximum nodes around 33,000. All working well with a subset of the actual Category/Product structure. On completion of basic testing, I switch on the display of the full Tree which configures and displays ok. I can expand and collapse the tree ok. However, part of the functionality in creating the tree is to allow the user to select all or a subset of the Category/Product nodes. My recursive code (to build a list of selected nodes) when it gets to around 7,500 nodes it generates the StackOverflow exception. I have 16GB of RAM - each node has around 200 characters of information associated with it. If my maths is correct I should be coming no where near maxing out my available memory. As you can see I have a terminating check at the beginning of my recursive function (at the crash point the vars are 7587 and 33159 respectively) so it is not in a recursive loop. I think my code is ok - does this look like a memory maxing out problem? BTW the 'TraverseDownProductTree' is identical to the shown code except for a test against "PROD" instead of "CAT". This works fine for small subsets of the tree (say 5000 nodes) so if it is a memory problem I will either have to cut down my 200 chars or buy more memory :-( thanks in advance
private void TraverseDownCategoryTree(int selectedTreeIndex, int lastSelectedTreeIndex)
{
if (selectedTreeIndex == (lastSelectedTreeIndex)) return;
TreeNode currentNode = TreeSelectedNodesList[selectedTreeIndex++];
if (currentNode.Name.StartsWith("CAT"))
{
int categoryId = ((LevelTag)currentNode.Tag).Id;
SelectedCategoriesList.Add(categoryId.ToString());
TraverseDownCategoryTree(selectedTreeIndex, lastSelectedTreeIndex);
}
else
{
// PROD nodes found so start creating the export file
selectedTreeIndex--; // set index for the traverse down the products starting at the proper node i.e. the node that is being processed here
TraverseDownProductTree(selectedTreeIndex, lastSelectedTreeIndex);
}
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
