diff options
-rw-r--r-- | docs/release-notes.md | 1 | ||||
-rw-r--r-- | src/SMAPI/Framework/Events/ManagedEvent.cs | 7 |
2 files changed, 5 insertions, 3 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md index 07905180..ed496848 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -11,6 +11,7 @@ * For players: * SMAPI now ignores more content file types when detecting mods (`.doc`, `.docx`, `.rar`, and `.zip`). * Fixed launcher's fallback logic on Linux when no compatible terminal was found (thanks to jlaw!). + * Fixed rare crash when a mod adds/removes an event handler from an event handler. * For the Console Commands mod: * Fixed error opening menu when some item data is invalid. diff --git a/src/SMAPI/Framework/Events/ManagedEvent.cs b/src/SMAPI/Framework/Events/ManagedEvent.cs index 08ac1131..8b25a9b5 100644 --- a/src/SMAPI/Framework/Events/ManagedEvent.cs +++ b/src/SMAPI/Framework/Events/ManagedEvent.cs @@ -106,19 +106,20 @@ namespace StardewModdingAPI.Framework.Events // update cached data // (This is debounced here to avoid repeatedly sorting when handlers are added/removed, // and keeping a separate cached list allows changes during enumeration.) - if (this.CachedHandlers == null) + var handlers = this.CachedHandlers; // iterate local copy in case a mod adds/removes a handler while handling the event + if (handlers == null) { if (this.HasNewHandlers && this.Handlers.Any(p => p.Priority != EventPriority.Normal)) this.Handlers.Sort(); - this.CachedHandlers = this.Handlers.ToArray(); + this.CachedHandlers = handlers = this.Handlers.ToArray(); this.HasNewHandlers = false; } // raise event this.PerformanceMonitor.Track(this.EventName, () => { - foreach (ManagedEventHandler<TEventArgs> handler in this.CachedHandlers) + foreach (ManagedEventHandler<TEventArgs> handler in handlers) { if (match != null && !match(handler.SourceMod)) continue; |