Asp. NET Crash-sitemap Madness in the Loop

Source: Internet
Author: User

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 ()

...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.