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

Update DesktopWindowXamlSource to describe how to avoid leaks #2054

Draft
wants to merge 7 commits into
base: docs
Choose a base branch
from
2 changes: 1 addition & 1 deletion windows.ui.xaml.hosting/desktopwindowxamlsource.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Equivalent WinUI class: [Microsoft.UI.Xaml.Hosting.DesktopWindowXamlSource](/win

If you create a **DesktopWindowXamlSource** object before you create the [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects that will be hosted in it, the framework for hosting [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) content makes sure all the objects are initialized to the same thread. If you create the [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects before you create the **DesktopWindowXamlSource** object in which they will be hosted, you must call [WindowsXamlManager.InitializeForCurrentThread](windowsxamlmanager_initializeforcurrentthread_14911797.md) before you instantiate the [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects.

Because **DesktopWindowXamlSource** derives from [IClosable](../windows.foundation/iclosable.md), so it is recommended that you **Close** it (**Dispose** it in .NET) when you’re finished with it.
**DesktopWindowXamlSource** derives from [IClosable](../windows.foundation/iclosable.md). To avoid a leak that results from a cycle between it and **WindowsXamlManager** you must call **Close** (**Dispose** it in .NET) before you release **WindowsXamlManager**.

## -see-also
[Using the UWP XAML hosting API in a desktop application](/windows/uwp/xaml-platform/using-the-xaml-hosting-api)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,25 @@ Equivalent WinUI method: [Microsoft.UI.Xaml.Hosting.WindowsXamlManager.Initializ
An object that contains a reference to the UWP XAML framework.

## -remarks
Call this method to initialize the internal UWP XAML framework for the current thread in a desktop application in which you want to host [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects in a [DesktopWindowXamlSource](desktopwindowxamlsource.md). You only need to explicitly call this method if your application creates the **Windows.UI.Xaml.UIElement** objects before it creates the **DesktopWindowXamlSource** object that will host them. Your application should typically should call this method when the parent UI object that hosts the **DesktopWindowXamlSource** is instantiated.
Call this method to initialize the internal UWP XAML framework for the current thread in a desktop application in which you want to host [Windows.UI.Xaml.UIElement](../windows.ui.xaml/uielement.md) objects in a [DesktopWindowXamlSource](desktopwindowxamlsource.md). Call this method if your application creates the **Windows.UI.Xaml.UIElement** objects before it creates the **DesktopWindowXamlSource** object that will host them, or if your program uses XAML from more than one thread.

If you create a **DesktopWindowXamlSource** object before you create the **Windows.UI.Xaml.UIElement** objects that will be hosted in it, you don’t need to call this method. In this scenario, the UWP XAML framework will be initialized for you when you instantiate the **DesktopWindowXamlSource** object.
This method must be called before creating the **DesktopWindowXamlSource** on the thread.

This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework. You can create as many **WindowsXamlManager** objects as you want on a given thread. However, because each object holds a reference to the UWP XAML framework, you should **Close** (**Dispose** in .NET) the objects to ensure that XAML resources are eventually released.
This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework for the current thread. It is safe to call this method multiple times, it will return the same instance if one is active.
You must destroy the instance after destroying the **DesktopWindowXamlSource** instances. Note, after releasing this object a message loop must be run to finish the resource rundown, otherwise the associated resources will be leaked.

```cpp

// Break the cycle between the WindowsXamlManager and the DesktopWindowXamlSource.
m_xamlSource.Close();
m_xamlManager = nullptr;

// Drain the message queue after releasing WindowsXamlManager since rundown is async
while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE))

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE))

{
::DispatchMessageW(&msg);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

    ::DispatchMessageW(&msg);

}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

```

## -see-also

Expand Down