{"record":{"id":"430e7f1e13ce7921","repo":"PrismLibrary/Prism","slug":"cannot-destroy-view","errorCode":null,"errorMessage":"Cannot destroy {view}.","messagePattern":"Cannot destroy (.+?)\\.","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"src/Maui/Prism.Maui/Common/MvvmHelpers.cs","lineNumber":77,"sourceCode":"    {\n        try\n        {\n            DestroyChildren(view);\n\n            InvokeViewAndViewModelAction<IDestructible>(view, v => v.Destroy());\n\n            //I'm actually not sure if this is necessary, but it seems like a good idea to clear the child regions of the page before we clear the behaviors and binding context of the page itself.\n            if (view is Page page)\n                page.ClearChildRegions();\n\n            if (view is VisualElement visualElement)\n            {\n                DeferredCleanup(visualElement);\n            }\n        }\n        catch (Exception ex)\n        {\n            throw new Exception($\"Cannot destroy {view}.\", ex);\n        }\n    }\n\n    private static void DeferredCleanup(VisualElement visualElement)\n    {\n        // Delay cleanup until after page transition animations have completed.\n        // We cannot clear BindingContext immediately because the page is still\n        // visible during the pop animation, causing a visual flicker.\n        // Neither Unloaded nor ParentChanged fire reliably across all platforms,\n        // so a dispatcher delay is the only consistent approach.\n        visualElement.Dispatcher?.DispatchDelayed(TimeSpan.FromMilliseconds(800), () =>\n        {\n            visualElement.Behaviors?.Clear();\n            visualElement.BindingContext = null;\n        });\n    }\n\n    private static void DestroyChildren(IView? page)","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/PrismLibrary/Prism/blob/358118cd640d9a22ff8cf21c8ad197fa038b7990/src/Maui/Prism.Maui/Common/MvvmHelpers.cs#L59-L95","documentation":"Prism.Maui's MvvmHelpers.DestroyPage cleans up a page (view and ViewModel, children and modals) and wraps the whole operation in a try/catch; any exception during cleanup is rethrown as Exception 'Cannot destroy {view}.' with the original as InnerException. It indicates a failure while tearing down a page during navigation.","triggerScenarios":"Calling MvvmHelpers.DestroyPage (directly or via DestroyWithModalStack/HandleSystemGoBack) on a page whose OnNavigatedFrom/IConfirmNavigationRequest/IDestructible.Destroy throws, or whose modal stack or child pages fail to clean up.","commonSituations":"ViewModel's Destroy implementation throws (null dependencies, disposed resources); a page in the modal stack fails cleanup; navigation racing with page transition animations; exception inside a child view's cleanup during DestroyChildren.","solutions":["Inspect the InnerException to find the real failing component (ViewModel Destroy, OnNavigatedFrom, child cleanup).","Wrap IDestructible.Destroy logic in the ViewModel with defensive null checks and its own try/catch.","Ensure pages in the modal stack and their ViewModels implement cleanup safely and can handle repeated calls.","Avoid throwing from IConfirmNavigationRequest/OnNavigatedFrom implementations."],"exampleFix":"// before\npublic void Destroy()\n{\n    _subscription.Dispose(); // throws if already disposed\n}\n// after\npublic void Destroy()\n{\n    _subscription?.Dispose();\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"bool SafeToDestroy(Page p) => p is not null && (p.BindingContext as IDestructible) is not null;","tryCatchPattern":"try { MvvmHelpers.DestroyPage(page); }\ncatch (Exception ex) when (ex.Message.StartsWith(\"Cannot destroy\"))\n{ Log(ex.InnerException); /* inspect real cause */ }","preventionTips":["Make IDestructible.Destroy implementations exception-free and idempotent","Use null-conditional disposal in ViewModels (? Dispose)","Inspect InnerException immediately — this error always wraps the real cause"],"tags":["maui","navigation","cleanup","page-lifecycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"358118cd640d9a22ff8cf21c8ad197fa038b7990","analyzedAt":"2026-09-15T15:00:31.079Z","contentChangedAt":"2026-09-15T15:00:31.079Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}