From 47e2c4806887f279d21abcc2b0d0157a5fc4322e Mon Sep 17 00:00:00 2001 From: RoosterDragon Date: Wed, 1 Apr 2015 20:33:51 +0100 Subject: [PATCH] Ensure MapCache disposes cleanly. This prevents the map loading thread writing to disposed objects which can have unintended side effects. --- OpenRA.Game/Map/MapCache.cs | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/OpenRA.Game/Map/MapCache.cs b/OpenRA.Game/Map/MapCache.cs index 81225afdef..0303c32795 100644 --- a/OpenRA.Game/Map/MapCache.cs +++ b/OpenRA.Game/Map/MapCache.cs @@ -250,7 +250,20 @@ namespace OpenRA public void Dispose() { - sheetBuilder.Dispose(); + if (previewLoaderThread == null) + { + sheetBuilder.Dispose(); + return; + } + + // We need to let the loader thread exit before we can dispose our sheet builder. + // Ideally we should dispose our resources before returning, but we don't to block waiting on the loader thread to exit. + // Instead, we'll queue disposal to be run once it has exited. + ThreadPool.QueueUserWorkItem(_ => + { + previewLoaderThread.Join(); + Game.RunAfterTick(sheetBuilder.Dispose); + }); } } }