{"record":{"id":"ca567e595b8f5473","repo":"dotnet/wpf","slug":"sr-hwndsourcedisposed","errorCode":null,"errorMessage":"SR.HwndSourceDisposed","messagePattern":"SR\\.HwndSourceDisposed","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSource.cs","lineNumber":2631,"sourceCode":"                    // handlers call methods on us.\n                    //\n                    // Note: as the HwndWrapper shuts down, the final few messages\n                    // will continue to pass through our WndProc hook.\n                    _isDisposed = true;\n                }\n            }\n        }\n\n        private void CheckDisposed(bool verifyAccess)\n        {\n            if(verifyAccess)\n            {\n//                 this.VerifyAccess();\n            }\n\n            if(_isDisposed)\n            {\n                throw new ObjectDisposedException(null, SR.HwndSourceDisposed);\n            }\n        }\n\n        private bool IsUsable\n        {\n            get\n            {\n                return !_isDisposed && _hwndTarget is not null && !_hwndTarget.IsDisposed;\n            }\n        }\n\n        private bool HasFocus\n        {\n            get\n            {\n                return UnsafeNativeMethods.GetFocus() == Handle;\n            }\n        }","sourceCodeStart":2613,"sourceCodeEnd":2649,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndSource.cs#L2613-L2649","documentation":"HwndSource members call CheckDisposed, which throws ObjectDisposedException(null, SR.HwndSourceDisposed) when the source has already been disposed (_isDisposed is true). It signals that the operation requires a live HwndSource whose HWND and resources still exist. The message identifies the disposed HwndSource instance.","triggerScenarios":"Calling any HwndSource API (AddHook, RemoveHook, SizeToContent setter, RegisterKeyboardInputSink, RootVisual, etc.) after Dispose was called or after the window was closed/disposed during shutdown.","commonSituations":"Event handlers or hooks firing during application shutdown after the window closed; async callbacks (Dispatcher.BeginInvoke) completing after disposal; cached HwndSource references (from FromHwnd at startup) used long after the window closed.","solutions":["Check hwndSource.IsDisposed before using the instance and bail out or re-obtain the source.","Unhook events and cancel pending Dispatcher operations when the window closes (override OnClosed) so callbacks don't touch a disposed source.","Re-resolve the source with HwndSource.FromHwnd(visual) at time of use rather than caching it, and handle null.","Wrap late callbacks in a liveness check on the associated Window (window.IsLoaded / window.IsDisposed equivalent)."],"exampleFix":"// before\nprivate void OnBackgroundWorkDone()\n{\n    _hwndSource.AddHook(WndProc); // throws if window closed\n}\n// after\nprivate void OnBackgroundWorkDone()\n{\n    if (_hwndSource == null || _hwndSource.IsDisposed)\n        return;\n    _hwndSource.AddHook(WndProc);\n}\n","handlingStrategy":"type-guard","validationCode":"if (source == null || source.IsDisposed) return;","typeGuard":"static bool IsUsable(HwndSource s) => s != null && !s.IsDisposed;","tryCatchPattern":"try\n{\n    _hwndSource.AddHook(WndProc);\n}\ncatch (ObjectDisposedException)\n{\n    // window already closed; skip or re-acquire source\n}","preventionTips":["Check IsDisposed before every use of a cached HwndSource.","Unhook events and cancel pending Dispatcher work in OnClosed.","Resolve the source fresh via HwndSource.FromHwnd at time of use instead of caching."],"tags":["disposed","lifecycle","wpf","window"],"backgroundTag":"object-disposed","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}