{"record":{"id":"5bc359419d7fb0b5","repo":"cefsharp/CefSharp","slug":"browser-has-been-disposed","errorCode":null,"errorMessage":"Browser has been disposed","messagePattern":"Browser has been disposed","errorType":"exception","errorClass":"ObjectDisposedException","httpStatus":null,"severity":"error","filePath":"CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs","lineNumber":1951,"sourceCode":"        /// </summary>\n        /// <exception cref=\"Exception\">Thrown when an exception error condition occurs.</exception>\n        private void ThrowExceptionIfBrowserNotInitialized()\n        {\n            if (!InternalIsBrowserInitialized())\n            {\n                throw new Exception(BrowserNotInitializedExceptionErrorMessage);\n            }\n        }\n\n        /// <summary>\n        /// Throw exception if disposed.\n        /// </summary>\n        /// <exception cref=\"ObjectDisposedException\">Thrown when a supplied object has been disposed.</exception>\n        private void ThrowExceptionIfDisposed()\n        {\n            if (IsDisposed)\n            {\n                throw new ObjectDisposedException(\"browser\", \"Browser has been disposed\");\n            }\n        }\n\n        private int GetChromiumChildProcessId(string frameIdentifier)\n        {\n            try\n            {\n                var parts = frameIdentifier.Split('-');\n\n                if (int.TryParse(parts[0], out var childProcessId))\n                    return childProcessId;\n            }\n            catch\n            {\n\n            }\n\n            return -1;","sourceCodeStart":1933,"sourceCodeEnd":1969,"githubUrl":"https://github.com/cefsharp/CefSharp/blob/16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec/CefSharp.Wpf/HwndHost/ChromiumWebBrowser.cs#L1933-L1969","documentation":"ThrowExceptionIfDisposed checks the IsDisposed flag (backed by an interlocked dispose counter). Once the ChromiumWebBrowser has been disposed — through window close, explicit Dispose, or CEF shutdown — any further API call that calls ThrowExceptionIfDisposed throws ObjectDisposedException with the object name 'browser'.","triggerScenarios":"Calling browser-dependent methods (CaptureScreenshotAsync, etc.) after Dispose has run, after the window/tab hosting the browser was closed, or after Cef.Shutdown.","commonSituations":"Async operations (e.g. an awaited screenshot or script evaluation) completing after the user closed the window. Event handlers firing during or after teardown. Timer-driven code that doesn't check disposal state.","solutions":["Check IsDisposed before calling any browser API.","Cancel pending async operations when the browser is disposed (use a CancellationToken).","Unsubscribe from events and stop timers in the window Closing handler before the browser is disposed."],"exampleFix":"// before — async op completes after close, throws\nvar shot = await browser.CaptureScreenshotAsync();\n\n// after — guard against disposal\nif (browser.IsDisposed) return;\nvar shot = await browser.CaptureScreenshotAsync();\n\n// or use a cancellation token tied to disposal\ncts.Token.ThrowIfCancellationRequested();","handlingStrategy":"validation","validationCode":"if (browser.IsDisposed)\n{\n    return; // or throw a domain-specific exception\n}\n// safe to proceed\nvar shot = await browser.CaptureScreenshotAsync();","typeGuard":null,"tryCatchPattern":"try\n{\n    var shot = await browser.CaptureScreenshotAsync();\n}\ncatch (ObjectDisposedException) when (browser.IsDisposed)\n{\n    // Browser was disposed during async op — gracefully ignore or log\n    Log.Info(\"Browser disposed before screenshot completed\");\n}","preventionTips":["Check IsDisposed before every browser API call, especially in async and event-driven code.","Cancel pending async operations when the browser is disposed.","Unsubscribe event handlers and stop timers in the Closing/Unloaded handler."],"tags":["disposal","lifecycle","object-disposed","wpf-hwndhost"],"backgroundTag":null,"analyzedSha":"16bc6e0711a4945be17e8bc4bc7ccf9469f3fdec","analyzedAt":"2026-08-13T19:57:05.828Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}