mirror of
https://github.com/microsoft/microsoft-ui-xaml.git
synced 2026-10-28 08:25:50 +08:00
## Fixes WinUI cannot be shut down and restarted in a process. This change fixes that problem, allowing for shut down and restart. Addresses #11741 ## PR Type - [x] Bugfix - [ ] Feature - [ ] Code style update (formatting, renaming) - [ ] Refactoring (no functional changes, no API changes) - [ ] Build related changes - [ ] Documentation content changes - [ ] Other (please describe): ## Description Second attempt to solve this problem. This one is simpler and does not involve new events on `WindowsXamlManager`. Thanks to Evelyn and Morten for the suggestion. Shutting down WinUI and restarting results in a crash in `MicaBackdrop`'s `Kind` property. The problem here is stale data. MUXC registers custom `DependencyProperty` objects (including `MicaBackdrop.Kind`) with MUX, and also has references to those DP objects. When MUX shuts down, it clears out all fields in its registered `CCustomDependencyProperty` objects, including those from MUXC. MUXC continues to hold on to its old and cleared out DP objects. When MUXC restarts, it sees that its caches are still filled, so it doesn't bother registering new custom DPs. It then tries to reuse its old `Kind` property, finds that the default value is now null, and crashes. The fix is for MUX to not clear out its registered `CCustomDependencyProperty` objects when shutting down. These types are agile and are valid on any UI thread. They are also potentially stored in `static readonly` fields, which means they can't be discarded and re-registered. In practice, both MUXC and the Windows Community Toolkit have custom DPs that can be reused across WinUI shutdown and restart. Two other changes as part of this PR: * Breaks a reference cycle that leaks the `WindowsXamlManager`. Previously it referenced a `XamlCore` object that referenced it back. * Updates WinUICppIslandsSampleApp to show a count of WinUI objects (Application, DispatcherQueueController, WindowsXamlManager, and DesktopWindowXamlSource). ### Current Behavior When WinUI shuts down we clear out all metadata and custom DPs. When WinUI restarts, external components (like MUXC) will find their previously registered custom DPs invalid and hit errors. After WinUI shuts down the `WindowsXamlManager` object will still be alive due to a leak. ### New Behavior When WinUI shuts down we leave all existing metadata and custom DPs alone. When WinUI restarts, external components (like MUXC) can continue using their custom DPs. Note that this has caveats for cleanup. The `Application` object now survives WinUI shutdown. Metadata objects also survive until process shutdown and technically leak, were it not for the entire process being torn down. Previously WinUI shutting down was where we cleared metadata. Now that metadata persists past shutdown, the next place that we were attempting to delete metadata is unloading the MUX binary, which only happens during process shutdown. But binary unload is not a safe place to do cleanup, because metadata objects may point to objects in other binaries which have already been unloaded. We choose to leak these objects and let the process take them down instead. When WinUI shuts down the `WindowsXamlManager` object will be released afterwards. ## Customer Impact Non-WASDK (e.g. Win32, WinForms) apps using Xaml islands can now release every single island, then re-create an island later. ## Regression Potential - [x] Low risk — isolated change, limited scope - [ ] Medium risk — touches shared components or public APIs - [ ] High risk — architectural or breaking API change ## How Has This Been Tested? - [x] I have performed a self-review of my own code - [x] I have added tests to cover my changes - [x] Existing tests pass locally ## Screenshots (if appropriate) <!-- If you are making visual changes, include before/after screenshots. --> <img width="1631" height="1346" alt="WinUICppIslandsSampleApp" src="https://github.com/user-attachments/assets/2f2ed871-1a1f-4ca1-8668-a2b18a5bb0d2" /> <img width="1368" height="1069" alt="WinUICsIslandsSampleApp" src="https://github.com/user-attachments/assets/459f0a12-5833-4282-9694-4e65952c97c5" />