{"record":{"id":"996b43c1d1c12b30","repo":"dotnet/wpf","slug":"no-message-invalidoperationexception","errorCode":null,"errorMessage":"(no message) InvalidOperationException()","messagePattern":"\\(no message\\) InvalidOperationException\\(\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs","lineNumber":353,"sourceCode":"                EventHandler enterMenuMode = EnterMenuMode;\n                if (null != enterMenuMode)\n                {\n                    enterMenuMode(null, EventArgs.Empty);\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Controls call this to leave menu mode.\n        ///</summary>\n        public void PopMenuMode(PresentationSource menuSite)\n        {\n            ArgumentNullException.ThrowIfNull(menuSite);\n            menuSite.VerifyAccess();\n\n            if (_menuModeCount <= 0)\n            {\n                throw new InvalidOperationException();\n            }\n\n            menuSite.PopMenuMode();\n            _menuModeCount -= 1;\n\n            if (0 == _menuModeCount)\n            {\n                EventHandler leaveMenuMode = LeaveMenuMode;\n                if (null != leaveMenuMode)\n                {\n                    leaveMenuMode(null, EventArgs.Empty);\n                }\n            }\n        }\n\n        /// <summary>\n        ///     Returns whether or not the input manager is in menu mode.\n        ///</summary>","sourceCodeStart":335,"sourceCodeEnd":371,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Input/InputManager.cs#L335-L371","documentation":"InputManager.PopMenuMode throws a parameterless InvalidOperationException when _menuModeCount is zero or negative, meaning PopMenuMode was called more times than PushMenuMode. Menu mode is a ref-counted nesting; popping without a matching push breaks the internal invariant that the stack of menu sites is balanced.","triggerScenarios":"Calling PopMenuMode on an HwndSource/InputManager pair without a preceding PushMenuMode, or calling it twice after one push, e.g. in custom popup/menu interop code or double-dispose logic.","commonSituations":"Custom menu/popup hosting code that pushes on one path but pops on several (error + cleanup paths both popping); re-entrant close handlers; interop with Win32 menus where WM_EXITMENULOOP arrives more than once.","solutions":["Balance every PopMenuMode with exactly one PushMenuMode; track your own nesting count if paths can overlap.","Check that cleanup/dispose logic does not call PopMenuMode redundantly (use a pushed flag).","Catch InvalidOperationException around the pop as a defensive guard in interop-heavy code, but fix the imbalance."],"exampleFix":"// before\ncloseHandler += () => site.PopMenuMode(); // may run twice\n// after\nif (!_menuPushed) return;\n_menuPushed = false;\nsite.PopMenuMode();","handlingStrategy":"try-catch","validationCode":"if (_menuModeCount <= 0) return; // track your own push count alongside InputManager's","typeGuard":null,"tryCatchPattern":"try { inputManager.PopMenuMode(menuSite); }\ncatch (InvalidOperationException) { /* unbalanced pop; log and continue */ }","preventionTips":["Maintain your own boolean/count so each PushMenuMode is popped exactly once.","Guard cleanup paths so error and close handlers do not both pop.","In menu interop, handle WM_EXITMENULOOP idempotently."],"tags":["wpf","input","menu","unbalanced-nesting"],"backgroundTag":"internal-invariant-violation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}