summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJesse Plamondon-Willard <github@jplamondonw.com>2017-04-23 23:10:52 -0400
committerJesse Plamondon-Willard <github@jplamondonw.com>2017-04-23 23:10:52 -0400
commite3219bd96988c5890e4ddfeb661c942c0c86d251 (patch)
tree3b7a084d1737b8bfb87987d3bf5f635a91e1598f
parenta9c220c0fe0fd6f52bee73a8f5da91fd1d007d0f (diff)
downloadSMAPI-e3219bd96988c5890e4ddfeb661c942c0c86d251.tar.gz
SMAPI-e3219bd96988c5890e4ddfeb661c942c0c86d251.tar.bz2
SMAPI-e3219bd96988c5890e4ddfeb661c942c0c86d251.zip
dispose resources on Windows Form exit (#268)
-rw-r--r--src/StardewModdingAPI/Program.cs17
1 files changed, 14 insertions, 3 deletions
diff --git a/src/StardewModdingAPI/Program.cs b/src/StardewModdingAPI/Program.cs
index 5cf80448..424e1727 100644
--- a/src/StardewModdingAPI/Program.cs
+++ b/src/StardewModdingAPI/Program.cs
@@ -61,6 +61,9 @@ namespace StardewModdingAPI
/// <summary>Whether the game is currently running.</summary>
private bool IsGameRunning;
+ /// <summary>Whether the program has been disposed.</summary>
+ private bool IsDisposed;
+
/*********
** Public methods
@@ -136,7 +139,7 @@ namespace StardewModdingAPI
#endif
AppDomain.CurrentDomain.UnhandledException += (sender, e) => this.Monitor.Log($"Critical app domain exception: {e.ExceptionObject}", LogLevel.Error);
- // override game & hook events
+ // override game
this.GameInstance = new SGame(this.Monitor);
#if SDV_1_2
StardewValley.Program.gamePtr = this.GameInstance;
@@ -148,7 +151,10 @@ namespace StardewModdingAPI
#endif
// hook into game events
- this.GameInstance.Exiting += (sender, e) => this.IsGameRunning = false;
+#if SMAPI_FOR_WINDOWS
+ ((Form)Control.FromHandle(this.GameInstance.Window.Handle)).FormClosing += (sender, args) => this.Dispose();
+#endif
+ this.GameInstance.Exiting += (sender, e) => this.Dispose();
this.GameInstance.Window.ClientSizeChanged += (sender, e) => GraphicsEvents.InvokeResize(this.Monitor, sender, e);
GameEvents.InitializeInternal += (sender, e) => this.InitialiseAfterGameStart();
GameEvents.GameLoaded += (sender, e) => this.CheckForUpdateAsync();
@@ -178,7 +184,7 @@ namespace StardewModdingAPI
}
finally
{
- this.IsGameRunning = false;
+ this.Dispose();
}
}
@@ -207,6 +213,11 @@ namespace StardewModdingAPI
/// <summary>Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.</summary>
public void Dispose()
{
+ if (this.IsDisposed)
+ return;
+ this.IsDisposed = true;
+
+ this.IsGameRunning = false;
this.LogFile?.Dispose();
this.ConsoleManager?.Dispose();
this.CancellationTokenSource?.Dispose();