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. Dismiss Notice

Question Android crashes, PC is fine

Discussion in 'Android' started by DrLulz, Jun 19, 2023.

  1. DrLulz

    DrLulz

    Joined:
    Nov 14, 2022
    Posts:
    11
    can someone explain to me why

    Code (CSharp):
    1.  
    2. long baseCode = 9520000000001;
    3. long tempBaseCode = 9520000000000;
    4.  
    5.         while (tempBaseCode < baseCode)
    6.         {
    7.              for (int i = 0; i < persistenVariableManager.foodItems.Count; i++)
    8.              {
    9.                    if (long.Parse(PersistenVariableManager.foodItems[i].ID) == baseCode)
    10.                    {
    11.                         baseCode++;
    12.                     }
    13.                     tempBaseCode++;
    14.              }
    15.           }
    16.  
    crashes on android MOST of the time (if worked a hand full of times for a while for some reason) but works perfectly fin when tested on PC?

    i tried it on two android phones and they both crash.

    debugging becomes kinda tricky when it works perfectly fine in the editor.

    what i'm trying to do here is save files starting at 9520000000001 and then saving to whatever number is closest to 9520000000001 without duplicates.

    (yes the number has to be that long and no, using a shorter number and int instead of long doesn't fix the crashing)
     
    Last edited: Jun 19, 2023
  2. Tomas1856

    Tomas1856

    Unity Technologies

    Joined:
    Sep 21, 2012
    Posts:
    3,659
    If persistenVariableManager.foodItems.Count is zero, then it will be endless loop.
     
  3. DrLulz

    DrLulz

    Joined:
    Nov 14, 2022
    Posts:
    11
    well yeah, but that would be the case on PC too.
    it only crashes on android.

    it also can't really be 0 because it's the first thing that gets loaded when the program starts.

    i've fixed it by using another, cleaner approach

    Code (CSharp):
    1. FoodItems food = new FoodItems();
    2. long baseCode = 9520000000001;
    3. List<string> IDList = new List<string>();
    4.  
    5.                 for (int i = 0; i < PersistenVariableManager.foodItems.Count; i++)
    6.                 {
    7.                     IDList.Add(PersistenVariableManager.foodItems[i].ID);
    8.                 }
    9.                 while (IDList.Contains(baseCode.ToString()))
    10.                 {
    11.                     baseCode++;
    12.                 }
    13.                 qarCode = baseCode.ToString();
    14.  
    that doesn't crash on android.
     
    Last edited: Jun 19, 2023
  4. Ryiah

    Ryiah

    Joined:
    Oct 11, 2012
    Posts:
    20,084
    It is worth noting however that your second approach can't have an infinite loop. That said I'm inclined towards the possibility of generated garbage overwhelming device memory. Your first approach can have more iterations (it can potentially reach infinite after all) and therefore can generate more garbage than your second.
     
    Last edited: Jun 20, 2023