Are there risks to optimizing code in C#?

后端 未结 8 877
粉色の甜心
粉色の甜心 2020-12-18 18:53

In the build settings panel of VS2010 Pro, there is a CheckBox with the label \"optimize code\"... of course, I want to check it... but being unusually cautious, I asked my

8条回答
  •  离开以前
    2020-12-18 19:42

    .net compiler optimization could cause bugs. happend to me today. took me a few hours to nail it. the code is:

    for (int i = 0; i < list.Count-1; i++) {
      list[i+1].DoSomeThing();
      //some code
      if (someCondition) {
        list.insert(i+1, new Item());
        i++;
      }
    }
    

    at some point, the list[i+1] is addressed as list[i], as if both both point to the same item. this bug was so wierd. the code ran well at debug mode, and at release mode, but when I ran it out side visual studio, ex. from the .exe file, the code crashed. only turning off the compiler optimization fixed it.

提交回复
热议问题