开发者

How to find recursion in your app?

My c# service go开发者_运维技巧t an internal .net execution error that points to recursion issue (e.g. stack overflow). The problem is that the service is pretty large, so I am having trouble finding where the recursion actually occurs.

Can someone with massive regex mojo hook me up with a search string that would find what I need?


This is an unanswerable question in the general case. Except for the most trivial examples (e.g. a function calling itself directly), there's no way to analyze a program and determine if recursion occurs. You'll just need to start hitting the debugger or other runtime tools.

This is an example of the halting problem.


A recursion is not easy to find in some situations like:

method1() {
  method2()
}

method2() {
  method1()
}

So a regex probably would not help you to find it unless it's a trivial case.


I agree a regexp isn't going to cut it here.

A more direct way would be to get a dump file and look at it to see where the exception was thrown.

Or you could look at a static analysis tool like NDepend to examine the programs flow.


How about using a profiling tool like RedGate's Ants profiler or dotTrace?

They both offer free trials. Just run the code with the profiler running and it will quickly show you where your time/memory is being spent.

I'd bet that your problem recursive function will stick out quite a bit.

Additionally, what error logging framework are you using? In case the answer is none, consider adopting one. This Question deals with the options. With a good system, you should be able to get the stack trace which, if you're lucky, may give you clues as to where the exception is occurring.


Attach to the service in the debugger and debug it properly. You will find this much easier than trying to search the code of any reasonably sized project.


The easiest way to do this is to get a stack trace of the thing that is crashing. The stack trace will look like this:

Blah
Foo
Baz
Hello
...
Frob
Frob
Frob
Frob
[several hundred more Frobs]
Frob
Frob
...
Frob
Something -- crash!

The "Frob" is the recursive function. :-)


In dotnet 5 your app will Tell you in the stderr output after it has crashed where the recursion happened.

Stack overflow.
Repeat 32128 times:
--------------------------------
   at SoExceptional.Program.RecursiveDeath()
--------------------------------
   at SoExceptional.Program.Main(System.String[])

Process finished with exit code -1,073,741,571.
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜