Skip to content

Commit

Permalink
fix: fixing crash when closing unity
Browse files Browse the repository at this point in the history
using unity Json too late causes crash, so using quitting instead
  • Loading branch information
James-Frowen committed Aug 7, 2022
1 parent debb172 commit cf2513a
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions Assets/Mirage.Profiler/Editor/SavedData.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.IO;
using Mirage.NetworkProfiler.ModuleGUI.Messages;
using Mirage.NetworkProfiler.ModuleGUI.UITable;
using UnityEditor;
using UnityEngine;

namespace Mirage.NetworkProfiler.ModuleGUI
Expand Down Expand Up @@ -80,11 +81,27 @@ internal class SaveDataLoader
private SaveDataLoader()
{
NetworkProfilerRecorder.AfterSample += AfterSample;
EditorApplication.quitting += quitting;
}

private void quitting()
{
Console.WriteLine("[Mirage.Profiler] quitting");
// save and clear references when quitting,
// this is needed because finialize is called after unity dll unloads so causes crash
SaveBoth();
_receiveData = null;
_sentData = null;
}

~SaveDataLoader()
{
NetworkProfilerRecorder.AfterSample -= AfterSample;
SaveBoth();
}

private void SaveBoth()
{
if (_receiveData != null)
Save(GetFullPath("Receive"), _receiveData);

Expand Down Expand Up @@ -158,7 +175,7 @@ public static string GetFullPath(string name)

public static void Save(string path, SavedData data)
{
// Debug.Log($"Save {path}");
Console.WriteLine($"[Mirage.Profiler] Save {path}");
CheckDir(path);

var text = JsonUtility.ToJson(data);
Expand All @@ -167,7 +184,7 @@ public static void Save(string path, SavedData data)

public static SavedData Load(string path)
{
// Debug.Log($"Load {path}");
Console.WriteLine($"[Mirage.Profiler] Load {path}");
CheckDir(path);

if (File.Exists(path))
Expand Down

0 comments on commit cf2513a

Please sign in to comment.