Search Unity

[Android]Should I ignore UnauthorizedAccessExceptions?

Discussion in 'Unity Cloud Diagnostics' started by MayhemMike, Nov 5, 2016.

  1. MayhemMike

    MayhemMike

    Joined:
    Oct 23, 2013
    Posts:
    51
    I have thousands of UnauthorizedAccessExceptions in my Game Performance log.

    Triggered by these lines:

    Code (CSharp):
    1. public static void Save()
    2.     {
    3.         savedGames = new SaveValues();
    4.         BinaryFormatter bf = new BinaryFormatter();
    5.         FileStream file = File.Create(Application.persistentDataPath + "/savedGames.gd");
    6.         bf.Serialize(file, SaveLoad.savedGames);
    7.         file.Close();
    8.     }
    Yet after 250 Beta feedbacks and countless inhouse tests everything is working as intended.
    The game saves and loads fine.

    Should I just ignore the logs or did I miss something?
     
  2. liortal

    liortal

    Joined:
    Oct 17, 2012
    Posts:
    3,562
    In our game, everything also works as intended, but we have many automated reports from different users (and devices) for file saving errors. These are really expected when you think of it (device runs out of space, for example).

    I believe your code should be updated with the following:
    1. Wrap all IO code (reading, writing files) in a try.. catch clause.
    2. Dispose of FileStream (it is derived from Stream, which is IDisposable).
    The code you posted would look like this with these modifications:
    Code (CSharp):
    1. public static void Save()
    2. {
    3.     savedGames = new SaveValues();
    4.  
    5.     BinaryFormatter bf = new BinaryFormatter();
    6.  
    7.     try
    8.     {
    9.         using (FileStream file = File.Create(Application.persistentDataPath + "/savedGames.gd"))
    10.         {
    11.             bf.Serialize(file, SaveLoad.savedGames);
    12.             file.Close();
    13.         }
    14.     }
    15.     catch (IOException ioException)
    16.     {
    17.         // Report the error, etc
    18.     }
    19. }
     
    MayhemMike likes this.
  3. MayhemMike

    MayhemMike

    Joined:
    Oct 23, 2013
    Posts:
    51
    thx liortal, will give it a try