Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice

While loop crashes the entire pc doing a pathfinding (a classic)

Discussion in 'Scripting' started by Luxgile, May 30, 2017.

  1. Luxgile

    Luxgile

    Joined:
    Nov 27, 2016
    Posts:
    16
    So i'm trying to make a pathfinder but somewhere something is wrong and i get an infinite loop. So i have 2 questions...

    1º I see people gets a crash in unity when this happens, but for me it crashes everything and i have to shutdown the pc. Don't really know if that's normal.

    2º I would be really glad if someone can find where the problem is, but the real point is the pc crashing.

    Code (CSharp):
    1. private void FindPath(Vector2 start, Vector2 end)
    2.     {
    3.         List<Point2> openPoints = new List<Point2>();
    4.         List<Point2> closedPoints = new List<Point2>();
    5.         List<Point2> path = new List<Point2>();
    6.  
    7.         Point2 startPoint = new Point2(start);
    8.         Point2 endPoint = new Point2(end);
    9.  
    10.         openPoints.Add(startPoint);
    11.         path.Add(startPoint);
    12.  
    13.         while(openPoints.Count > 0)
    14.         {
    15.             Point2 point = openPoints[0];
    16.             point.SetCost(startPoint, endPoint);
    17.             Debug.Log("Point is" + point.worldPos);
    18.  
    19.             for(int i = 1; i<openPoints.Count; i++)
    20.             {
    21.                 if(openPoints[i].fCost < point.fCost || openPoints[i].fCost == point.fCost)
    22.                 {
    23.                     if(openPoints[i].hCost < point.hCost)
    24.                     {
    25.                         point = openPoints[i];
    26.                         path.Add(point);
    27.                     }
    28.                 }
    29.             }
    30.  
    31.             openPoints.Remove(point);
    32.             closedPoints.Add(point);
    33.  
    34.             if(point == endPoint)
    35.             {
    36.                 return;
    37.             }
    38.  
    39.             foreach (Point2 nearPoint in point.GetNearPoints()){
    40.                 for(int i = 0; i<closedPoints.Count; i++){
    41.                     if(nearPoint.worldPos == closedPoints[i].worldPos){
    42.                         closedPoints.Add(nearPoint);
    43.                     }
    44.                 }
    45.                
    46.                 if(!closedPoints.Contains(nearPoint)){
    47.                     nearPoint.IsWalkeable();
    48.  
    49.                     if(nearPoint.walkeable){
    50.                         if(!openPoints.Contains(nearPoint)){
    51.                             nearPoint.SetCost(startPoint, endPoint);
    52.                             openPoints.Add(nearPoint);
    53.                         }
    54.                     }
    55.                 }
    56.             }
    57.  
    58.         }
    59.        
    60.         for(int i =0; i<path.Count; i++){
    61.             Instantiate(pointPrefab, path[i].worldPos, Quaternion.identity);
    62.         }
    63.        
    64.  
    65.     }
     
  2. BlackPete

    BlackPete

    Joined:
    Nov 16, 2016
    Posts:
    970
    Bringing down the PC does seem abnormal. I've actually tried to force a crash using a recursive loop and the most I've gotten was having Unity throw a stack overflow exception. So this is pretty unusual!

    However, since you're instantiating a prefab, I suspect this is the real culprit that's bringing your PC down to its knees when you run out of heap memory. Still, even in that case, the worst I've seen was having the application crash with an apologetic "Oops!" message (if it's a standalone build), or an error reporting form appears (if it's the editor that crashed).

    Still, if you've starved the OS itself of memory, then I could see that bringing the PC down to its knees.

    As a quick workaround, I'd maybe add some sort of a counter where if you reach a certain count, force an assert saying "Hmm, this count is suspiciously high. Aborting now." Or something to that effect.

    As for the algorithm itself... my brain is too tired to puzzle it out tonight, so I'll revisit this tomorrow :D
     
  3. Luxgile

    Luxgile

    Joined:
    Nov 27, 2016
    Posts:
    16
    I don't think it's the instantiate, i mean i have done a lots of loops while and every of them crashes the pc if they are infinite. But the count idea could work so i'll use it for now, thanks!!

    Hope for someone who can say something about the pc crashing. It's really annoying not just for the work lost but the time you lose restarting the pc.

    Edit: Even if i write a
    "int n = 0"
    and in the loop a
    "n++;
    if(n>5){break;}"

    doesn't crash the pc but it freezes several seconds and then it recovers. Anyways i have to close unity or otherwise i have an insane lag, maybe i wrote something bad or i'm misunderstanding something but i don't think it's normal to lag that much for a loop that should stop at the 5th time
     
    Last edited: May 31, 2017