From 96da7c1cbc19e079e06fe8c7c857ffe86c0d9848 Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Mon, 3 Jul 2017 14:49:29 -0400 Subject: fix crash in new content manager when returning to title (#255) --- src/StardewModdingAPI/Framework/SContentManager.cs | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'src/StardewModdingAPI/Framework') diff --git a/src/StardewModdingAPI/Framework/SContentManager.cs b/src/StardewModdingAPI/Framework/SContentManager.cs index ebf1c8a5..42c3b0e6 100644 --- a/src/StardewModdingAPI/Framework/SContentManager.cs +++ b/src/StardewModdingAPI/Framework/SContentManager.cs @@ -349,5 +349,23 @@ namespace StardewModdingAPI.Framework yield return new KeyValuePair(metadata, interceptor); } } + + /// Dispose all game resources. + /// Whether the content manager is disposing (rather than finalising). + protected override void Dispose(bool disposing) + { + if (!disposing) + return; + + // Clear cache & reload all assets. While that may seem perverse during disposal, it's + // necessary due to limitations in the way SMAPI currently intercepts content assets. + // + // The game uses multiple content managers while SMAPI needs one and only one. The game + // only disposes some of its content managers when returning to title, which means SMAPI + // can't know which assets are meant to be disposed. Here we remove current assets from + // the cache, but don't dispose them to avoid crashing any code that still references + // them. The garbage collector will eventually clean up any unused assets. + this.Reset(); + } } } -- cgit