{"record":{"id":"af3dd070d874b21b","repo":"stride3d/stride","slug":"an-error-occurred-while-disposing-the-window-manager","errorCode":null,"errorMessage":"An error occurred while disposing the window manager.","messagePattern":"An error occurred while disposing the window manager\\.","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowManager.cs","lineNumber":77,"sourceCode":"        /// Gets the current main window.\n        /// </summary>\n        public static WindowInfo MainWindow { get; private set; }\n\n        /// <summary>\n        /// Gets the collection of currently visible modal windows.\n        /// </summary>\n        public static IReadOnlyList<WindowInfo> ModalWindows => ModalWindowsList;\n\n        /// <summary>\n        /// Gets the collection of currently visible blocking windows.\n        /// </summary>\n        public static IReadOnlyList<WindowInfo> BlockingWindows => BlockingWindowsList;\n\n        /// <inheritdoc/>\n        public void Dispose()\n        {\n            if (!NativeHelper.UnhookWinEvent(hook))\n                throw new InvalidOperationException(\"An error occurred while disposing the window manager.\");\n\n            hook = IntPtr.Zero;\n            winEventProc = null;\n            dispatcher = null;\n            MainWindow = null;\n            AllWindowsList.Clear();\n            ModalWindowsList.Clear();\n            BlockingWindowsList.Clear();\n\n            Logger.Info($\"{nameof(WindowManager)} disposed\");\n            initialized = false;\n        }\n\n        /// <summary>\n        /// Shows the given window as the main window of the application. It is mandatory to use this method for the main window to use features of the <see cref=\"WindowManager\"/>.\n        /// </summary>\n        /// <param name=\"window\">The main window to show.</param>\n        public static void ShowMainWindow([NotNull] Window window)","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/stride3d/stride/blob/96fad776d210c221682aac1ccdf4c79dc046fc38/sources/presentation/Stride.Core.Presentation.Wpf/Windows/WindowManager.cs#L59-L95","documentation":"Dispose unhooks the native WinEvent hook via NativeHelper.UnhookWinEvent. If the unhook call returns false, cleanup failed and the method throws InvalidOperationException so callers know the manager was not fully torn down and native resources may still be hooked.","triggerScenarios":"Calling Dispose after the hook was already invalidated or the native unhook failed (invalid hook handle, wrong thread context, or double-dispose scenarios where the handle is stale).","commonSituations":"Disposing from a different thread/context than the hook was installed on; double disposal or disposing after hook corruption; native layer returning failure under shutdown races.","solutions":["Dispose on the same thread that constructed the WindowManager (the dispatcher thread)","Ensure Dispose is called exactly once (guard with a disposed flag or use try/finally)","If unhook fails on shutdown, log and continue — wrap Dispose in try/catch since the process is tearing down anyway"],"exampleFix":"// before\nmanager.Dispose(); manager.Dispose(); // second call: hook already zeroed/invalid\n// after\nif (manager != null) { manager.Dispose(); manager = null; }","handlingStrategy":"try-catch","validationCode":"if (manager == null || manager.IsDisposed) return;","typeGuard":null,"tryCatchPattern":"try { manager.Dispose(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"disposing the window manager\")) { /* log; shutdown race */ }","preventionTips":["Dispose on the constructing thread only","Dispose exactly once","Treat dispose failures during shutdown as non-fatal"],"tags":["wpf","native-interop","dispose"],"backgroundTag":"invalid-state-transition","analyzedSha":"96fad776d210c221682aac1ccdf4c79dc046fc38","analyzedAt":"2026-09-14T02:59:31.279Z","contentChangedAt":"2026-09-14T02:59:31.279Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}