From bf2a07454711fe27b6c39d4627b94d1b0c9765ef Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Mon, 19 Feb 2018 14:36:07 +0000 Subject: [PATCH] Added finalizer, less chance of crashing with unity editor stop plays missing dispose --- Facepunch.Steamworks/BaseSteamworks.cs | 9 +++++++++ Facepunch.Steamworks/Client.cs | 7 +++++++ Facepunch.Steamworks/Server.cs | 7 +++++++ 3 files changed, 23 insertions(+) diff --git a/Facepunch.Steamworks/BaseSteamworks.cs b/Facepunch.Steamworks/BaseSteamworks.cs index 036432d..9df2019 100644 --- a/Facepunch.Steamworks/BaseSteamworks.cs +++ b/Facepunch.Steamworks/BaseSteamworks.cs @@ -26,6 +26,7 @@ public class BaseSteamworks : IDisposable private List CallbackHandles = new List(); private List CallResults = new List(); + protected bool disposed = false; protected BaseSteamworks( uint appId ) @@ -39,8 +40,15 @@ protected BaseSteamworks( uint appId ) System.Environment.SetEnvironmentVariable("SteamGameId", AppId.ToString()); } + ~BaseSteamworks() + { + Dispose(); + } + public virtual void Dispose() { + if ( disposed ) return; + Callbacks.Clear(); foreach ( var h in CallbackHandles ) @@ -81,6 +89,7 @@ public virtual void Dispose() System.Environment.SetEnvironmentVariable("SteamAppId", null ); System.Environment.SetEnvironmentVariable("SteamGameId", null ); + disposed = true; } protected void SetupCommonInterfaces() diff --git a/Facepunch.Steamworks/Client.cs b/Facepunch.Steamworks/Client.cs index badfc21..f2feda0 100644 --- a/Facepunch.Steamworks/Client.cs +++ b/Facepunch.Steamworks/Client.cs @@ -135,6 +135,11 @@ public Client( uint appId ) : base( appId ) Update(); } + ~Client() + { + Dispose(); + } + /// /// Should be called at least once every frame /// @@ -163,6 +168,8 @@ public void RunCallbacks() /// public override void Dispose() { + if ( disposed ) return; + if ( Voice != null ) { Voice = null; diff --git a/Facepunch.Steamworks/Server.cs b/Facepunch.Steamworks/Server.cs index f4947ee..5deb113 100644 --- a/Facepunch.Steamworks/Server.cs +++ b/Facepunch.Steamworks/Server.cs @@ -83,6 +83,11 @@ public Server( uint appId, ServerInit init) : base( appId ) Update(); } + ~Server() + { + Dispose(); + } + /// /// Should be called at least once every frame /// @@ -256,6 +261,8 @@ public void UpdatePlayer( ulong steamid, string name, int score ) /// public override void Dispose() { + if ( disposed ) return; + if ( Query != null ) { Query = null;