One day, I received a letter about my blog, asked the following questions, briefly as follows:
I wanted to quickly create a sitemap, so I rewrote the BuildSiteMap () method, where I wrote a loop to add some fake sitemap nodes.
public override SiteMapNode BuildSiteMap(){
for (int i = 0; i < 5; i++)
myRoot.ChildNodes.Add(new SiteMapNode(this, i.ToString(), i.ToString(), i.ToString()));
return myRoot;
}
When you run the program, a stack overflow occurs and the server crashes. I used the debugger to step through and find it really strange:
1) int i = 0
2) i < 5
3) myRoot...
4) int i = 0
5) i < 5
etc.
The value of I never seems to increase unless I call the SiteMapNode (Access a property, called a method), which seems to be the correct loop.
What makes this cycle uncertain? I think it might be a compiler or a bug in the CLR.
(When I got this problem, I really didn't know the site navigation in asp.net2.0, but I found these articles ... http://weblogs.asp.net/scottgu/archive/2005/11/20/431019.aspx and HTTP ://aspnet.4guysfromrolla.com/articles/111605-1.aspx, the narration is really very good.
The original idea
The most important thing about this problem is that it always starts all over again, which means that it can be debugged on the spot. But we do not go so far, first look back to see what is now ...
1. Stack Overflow
2. A cycle that starts again and again
I've discussed the stack overflow in my previous blog post and now repeat ... The reason for the stack overflow is that too many function pointers, variable pointers, and arguments are allocated so that the amount of memory requested in the stack is not sufficient. So far, the most common cause of stack overflows is a recursive recursion that is not terminated. In other words, function A invokes function B, and function B invokes function a ...
So, CallStack looks a bit like this ....
...
functionB()
functionA()
functionB()
functionA()
Well, everything is great, but that only explains the stack overflow. What about the crazy cycle?
Good... Imagine having such a function (there is a breakpoint at the-->)
void MyRecursiveFunction(){
for(int i=0; i<5; i++){
--> MyRecursiveFunction();
}
}
When you first stop at a breakpoint, the value of I should be 0,callstack looks like this ...
Myrecursivefunction ()
...