- Home /
How to debug infinite loop in Visual Studio?
Hi, I very often stuck in infinite loops (not doing basic loops but while writing relatively complex and interlinked logic) and I never able to understand how to stop them in visual studio. I mean here to STOP the infinite loop when it starts executing and freezes unity and I don't mean to avoid them. Sometimes I know what loop caused problem but sometimes, in a lot of scripts, I don't know what loop is causing the problem of infinite loop. I have watched tutorials and read this blog Breakout! How to stop an infinite loop in a Unity C# script but I am always confused at step when I need to find cmp dword ptr [r11], 0. I don't understand a way to get out when the game freezes due to some unknown infinite loop, how to get it back and debug what loop caused problem. Is there any way to do this in Visual Studio (in C#)? Regards,
Infinite loops are always caused by the same set of mistakes. i.e. not conditionally breaking out of the loop.
If you are doing complex and interlinked logic inside a loop then the chances are you are probably doing something wrong. Can you post the code example of the loop (and its surrounding context) thats causing the problem please?
Hi, thanks for your response. As I have mentioned, I can avoid loops and I have debugged the problematic loop after placing some loop breaking conditions. But what I want to know instead of AVOIDING, how we can STOP infinite loops once we are in the loop. Let's take a very simple example, I have written let say
int i = 0; while (i < 10){ //do some stuff }
And here I forgot to increment i and hit play. As soon as I did it, unity freezes with some of my changes unsaved may be. I want to now break this loop so that I can identify where the problem was. But I have no other option then force quit unity. In such a situation, once we are in by mistake, how to get out without quitting unity?