I have the following recursive code and i am getting a stackoverflow exception. I can't figure out the root cause because once i get the exception,i dont get the full call stack in Visual studio.
The idea is that there are org teams that roll up into larger "Main" teams.
Does anyone see a flaw on this code below that could be the culprit?
private Unit GetUnit(Unit organisationalUnit)
{
if (organisationalUnit.IsMainUnit)
{
return organisationalUnit;
}
if (organisationalUnit.Parent == null)
return null;
return GetUnit(organisationalUnit.Parent);
}
IsMainUnit
and Parent
don't call GetUnit
.Does the root always have Parent == null
?
Tried checking
if (organisationalUnit.Parent == organisationalUnit)
return null;
?
You could try this to debug it better. It won't affect your production code.
using System.Diagnostics;
private Unit GetUnit(Unit organisationalUnit, int depth)
{
debug.assert(depth < 10, "Reached an unexpected high recursion depth");
if (organisationalUnit.IsMainUnit)
{
return organisationalUnit;
}
if (organisationalUnit.Parent == null)
return null;
return GetUnit(organisationalUnit.Parent, depth + 1);
}
private Unit GetUnit(Unit organisationalUnit)
{
return GetUnit(organisationalUnit.Parent, 0);
}
On second thought...
It's mostlikely that you have a circular reference somewhere.
A.parent = B;
B.parent = C;
C.parent = A;
You could try to pass a set of previous visited nodes and check whether or not you've visited this node before.
The thing with recursion is that you have to be sure that it will end and an unchecked circular reference is a situation where it wouldn't end.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With