8

I have a recursive function in a BaseClass which relies on a protected virtual function for it's return condition.

It's possible for a child class to override this function incorrectly and leads to a StackOverFlow exception. The worst thing is there are some slow network calls and the exception is not gonna happen soon (many resources waste for a long time).

I'm looking for a method to check StackOverFlow in early stages some way in the base class (maybe using Reflection and the current recursion level).

Any idea ?

1
  • ^vote for topic about stackoverflow. Commented Dec 24, 2014 at 13:10

3 Answers 3

8

You could pass a simple integer 'depth' to the recursive function and increment it with each subsequent call. If it gets larger than the maximum allowed depth throw an Exception right then instead of waiting until it's too late and the dreaded StackOverflow exception has occurred.

Such safety mechanisms (increment counter, check it's not stupidly large) can also be handy in while loops where a small error can cause an infinite loop consuming vast quantities of CPU.

In large systems with many users (e.g. web sites) sometimes it's best to take precautionary measures like these with recursion and while loops because the consequences can reach far beyond one web page or one user of the system. It's not pretty code and the purists will no doubt balk at it, but it's efficient, it's defensive and it's pragmatic.

Sign up to request clarification or add additional context in comments.

3 Comments

No way, Ian Mercer posts here?
bool RecursiveConditionMethod(ref int depthLevel) { depthLevel++; // if (depthLevel > MAX_DEPTH_LEVEL) break the loop is it OK ?
@Xaqron, I wouldn't use ref int like that. I would just pass depth+1 when the method calls itself and check the depth first thing inside the method. In a tree search for example you might not want to limit total nodes visited but you might want to ensure it doesn't get stuck in some loop because the tree wasn's a proper tree.
1

Solve the problem instead of creating a workaround. Create a private function which is recursive which calls the protected virtual function.

3 Comments

No way. Return condition is not revealed to base class until run-time and this is provided by child class via overriding that method.
?Why should your base class be dependent on a child classes overridden implementation?
Child classes are plug-ins sharing a lot of work via base class. If a child need to go in depth it, it's allowed but the code itself is inside the base class and shared among children.
0

Although you probably can read the call stack and analyze it I wouldn't do that.

  1. It will slow down execution
  2. It is not your base class' responsibility
  3. Document your base class' behaviour

An alternative could be to do the call stack analysis in DEBUG mode only. Here is a little code to see how to get the call stack.

using System.Diagnostics;

[STAThread]
public static void Main()
{
  StackTrace stackTrace = new StackTrace();           // get call stack
  StackFrame[] stackFrames = stackTrace.GetFrames();  // get method calls (frames)

  // write call stack method names
  foreach (StackFrame stackFrame in stackFrames)
  {
    Console.WriteLine(stackFrame.GetMethod().Name);   // write method name
  }
}

From this site

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.