microsoft / microsoft/microsoft-ui-xaml
C++: Can't `UnloadObject` after removing it from visual tree
- Dominant language
- C++
- Stars
- 8.4k
- Forks
- 942
- Avg merge
- 2d 7h
- Merged PRs (30d)
- 105
Description
### Describe the bug
I get an exception if I Unload an object after having removed from the visual tree.
This is only an issue in C++. I could not reproduce this in C#.
Is there a work around?
### Steps to reproduce the bug
```xaml
Click Me
```
```cpp
void MainWindow::myButton_Click(IInspectable const&, RoutedEventArgs const&)
{
myStackPanel().Children().ReplaceAll({ myButton() });
UnloadObject(myButton());
}
```
> Microsoft.UI.Xaml.dll!00007FF60A67D0F3: 80070057 - E_INVALIDARG
Exception thrown at 0x00007FF881B12BDC (KernelBase.dll) in App2.exe: WinRT originate error - 0x80070057 : 'The parameter is incorrect.'.
Exception thrown at 0x00007FF881B12BDC in App2.exe: Microsoft C++ exception: winrt::hresult_invalid_argument at memory location 0x00000038262F9FD8.
Exception thrown at 0x00007FF881B12BDC in App2.exe: Microsoft C++ exception: [rethrow] at memory location 0x0000000000000000.
A breakpoint instruction (__debugbreak() statement or a similar call) was executed in App2.exe.
### Expected behavior
_No response_
### Screenshots
_No response_
### NuGet package version
WinUI 3 - Windows App SDK 1.6.2: 1.6.241106002
### Windows version
Windows 11 (21H2): Build 22000
### Additional context
_No response_
Contributor guide
Research direction
Reproduce the issue using the provided XAML and C++ click handler with Windows App SDK 1.6.2 on Windows 11. Compare removing myButton with ReplaceAll followed by UnloadObject against the C# behavior; done means the C++ path no longer raises E_INVALIDARG or has a documented, working resolution.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp
- Domain
- desktop, operating-systems
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 25/100