Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++: Can't UnloadObject after removing it from visual tree #10176

Open
tom-huntington opened this issue Nov 18, 2024 · 2 comments
Open

C++: Can't UnloadObject after removing it from visual tree #10176

tom-huntington opened this issue Nov 18, 2024 · 2 comments
Labels
area-CoreFramework Issues on fundamental components like UIElement, visibility, etc. area-DevInternal Internal build infrastructure, code cleanup, engineering efficiency bug Something isn't working

Comments

@tom-huntington
Copy link

tom-huntington commented Nov 18, 2024

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

<Window ...>
    <StackPanel x:Name="myStackPanel" Orientation="Vertical"  VerticalAlignment="Center">
            <Button x:Load="True" x:Name="myButton" Click="myButton_Click">Click Me</Button>
    </StackPanel>
</Window>
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

@tom-huntington tom-huntington added the bug Something isn't working label Nov 18, 2024
@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Nov 18, 2024
@tom-huntington tom-huntington changed the title Can't Unload element after removing it from visual tree Can't UnloadObject after removing it from visual tree Nov 18, 2024
@kmgallahan
Copy link
Contributor

kmgallahan commented Nov 18, 2024

Per the x:Load documentation:

You can use x:Load to optimize the startup, visual tree creation, and memory usage of your XAML app. Using x:Load has a similar visual effect to Visibility, except that when the element is not loaded, its memory is released and internally a small placeholder is used to mark its place in the visual tree.

When an object is unloaded, it will be replaced in the tree with a placeholder. The object instance will remain in memory until all references have been released. The UnloadObject API on a Page/UserControl is designed to release the references held by codegen for x:Name and x:Bind. If you hold additional references in app code they will also need to be released.

UnloadObject sets x:Load. It is meant to optimize performance for objects in the visual tree, not as a "Dipsose" type of method to call on things you no longer want in the tree - which is what you are doing.

@tom-huntington
Copy link
Author

tom-huntington commented Nov 18, 2024

@kmgallahan

not as a "Dipsose" type of method to call on things you no longer want in the tree - which is what you are doing.

I want is a way to

to release the references held by codegen for x:Name

I'm only using it to remove things from the visual tree because that's the only situation when UnloadObject will work: i.e. It only works on objects (myButton) that haven't been previously removed from the visual tree.

I'm happy to manually remove the named elements, I'm trying to unload, from the visual tree

myStackPanel().Children().ReplaceAll({ });

but UnloadObject(myButton()); stills throws when I try to use it to release the references held by myButton()/x:Name.

@tom-huntington tom-huntington changed the title Can't UnloadObject after removing it from visual tree C++: Can't UnloadObject after removing it from visual tree Nov 18, 2024
@karkarl karkarl added area-CoreFramework Issues on fundamental components like UIElement, visibility, etc. area-DevInternal Internal build infrastructure, code cleanup, engineering efficiency and removed needs-triage Issue needs to be triaged by the area owners labels Dec 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-CoreFramework Issues on fundamental components like UIElement, visibility, etc. area-DevInternal Internal build infrastructure, code cleanup, engineering efficiency bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants