{"record":{"id":"f6e9a9a58e856f83","repo":"dotnet/wpf","slug":"sr-cantsetownertoclosedwindow","errorCode":null,"errorMessage":"SR.CantSetOwnerToClosedWindow","messagePattern":"SR\\.CantSetOwnerToClosedWindow","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":1241,"sourceCode":"                // is not allowed\n                VerifyApiSupported();\n                VerifyContextAndObjectState();\n                if (value == this)\n                {\n                    throw new ArgumentException(SR.CannotSetOwnerToItself);\n                }\n\n                if (_showingAsDialog)\n                {\n                    throw new InvalidOperationException(SR.CantSetOwnerAfterDialogIsShown);\n                }\n\n                if (value != null && value.IsSourceWindowNull)\n                {\n                    // Try to be specific in the error message.\n                    if (value._disposed)\n                    {\n                        throw new InvalidOperationException(SR.CantSetOwnerToClosedWindow);\n                    }\n                    throw new InvalidOperationException(SR.CantSetOwnerWhosHwndIsNotCreated);\n                }\n\n                if ( _ownerWindow == value )\n                {\n                    return;\n                }\n\n                if (!_disposed)\n                {\n                    // Check to see if value is already a child of this window.\n                    // If yes, throw Exception\n                    if (value != null)\n                    {\n                        WindowCollection ownedWindows = OwnedWindows;\n                        for (int i = 0; i < ownedWindows.Count; i++)\n                        {","sourceCodeStart":1223,"sourceCodeEnd":1259,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L1223-L1259","documentation":"Window.Owner setter throws InvalidOperationException when the candidate owner window has already been closed and disposed (_disposed == true). A disposed window has no valid HWND and cannot serve as an owner. The check is inside the Owner setter when value.IsSourceWindowNull is true.","triggerScenarios":"Assigning window.Owner to a Window instance that was already closed (Close called and window disposed).","commonSituations":"Caching a parent window reference that the user closed earlier, then using it to open a new dialog; long-lived references to closed windows in singleton patterns.","solutions":["Verify the candidate owner is alive before assigning: check !owner.IsDisposed (track via Closed event) or Application.Current.Windows.Contains(owner).","Pick a live window such as Application.Current.MainWindow as owner.","Set Owner before the candidate window can be closed, or drop ownership (leave Owner null)."],"exampleFix":"// before\ndlg.Owner = _cachedParent; // may be closed\n// after\nif (_cachedParent != null && Application.Current.Windows.Contains(_cachedParent))\n    dlg.Owner = _cachedParent;","handlingStrategy":"validation","validationCode":"bool isValidOwner(Window w) => w != null && Application.Current.Windows.Contains(w);","typeGuard":"bool IsLiveWindow(Window w) => w != null && !w.IsDisposed(); // track via Closed event flag","tryCatchPattern":"try { dlg.Owner = candidate; }\ncatch (InvalidOperationException) { dlg.Owner = Application.Current.MainWindow; }","preventionTips":["Unsubscribe from and null out references to windows in their Closed event.","Prefer Application.Current.MainWindow or the active window as owner instead of cached references.","Avoid singletons holding Window references."],"tags":["wpf","window","owner","disposed"],"backgroundTag":"invalid-argument-value","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"}