From abca97e0354475fcd430509533b122ee05393ecb Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 21 Jun 2021 17:57:49 -0700 Subject: [PATCH 1/7] DesktopWindowXamlSource.Close is required to be called to avoid leaks --- windows.ui.xaml.hosting/desktopwindowxamlsource.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows.ui.xaml.hosting/desktopwindowxamlsource.md b/windows.ui.xaml.hosting/desktopwindowxamlsource.md index d5410ddfcb..158fbd3a81 100644 --- a/windows.ui.xaml.hosting/desktopwindowxamlsource.md +++ b/windows.ui.xaml.hosting/desktopwindowxamlsource.md @@ -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) From 9ef1b6dc06ceab09639dbd6d97c06d6f0e182018 Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 21 Jun 2021 18:12:23 -0700 Subject: [PATCH 2/7] updates to WindowsXamlManager --- ...nager_initializeforcurrentthread_14911797.md | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index 285b38c61d..8fef76f4cf 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -19,11 +19,22 @@ 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 your program uses more than one thread that uses Xaml. Call this method before creating the **DesktopWindowXamlSource**. -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 returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework. Call this method as many times as you want but generally get one instance and run it down after running down the **DesktopWindowXamlSource**. Note, after releasing this object a message loop must be run to finish the resource rundown, otherwise the assocaited resources will be leaked. -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. +```cpp + + // Break the cycle between the WindowsXamlManager and the DesktopWindowXamlSource. + m_xamlSource.Close(); + m_xamlManager = nullptr; + + // Drain the message queue after releasign WindowsXamlManager since rundown is async + while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) + { + ::DispatchMessageW(&msg); + } +``` ## -see-also From d3a7f9ba4702d5840de1c8766fd71aef6b21d469 Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 21 Jun 2021 18:13:55 -0700 Subject: [PATCH 3/7] Update windowsxamlmanager_initializeforcurrentthread_14911797.md --- ...ager_initializeforcurrentthread_14911797.md | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index 8fef76f4cf..3a06807a65 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -25,15 +25,15 @@ This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that co ```cpp - // Break the cycle between the WindowsXamlManager and the DesktopWindowXamlSource. - m_xamlSource.Close(); - m_xamlManager = nullptr; - - // Drain the message queue after releasign WindowsXamlManager since rundown is async - while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) - { - ::DispatchMessageW(&msg); - } +// Break the cycle between the WindowsXamlManager and the DesktopWindowXamlSource. +m_xamlSource.Close(); +m_xamlManager = nullptr; + +// Drain the message queue after releasign WindowsXamlManager since rundown is async +while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) +{ + ::DispatchMessageW(&msg); +} ``` ## -see-also From e0535567db3ee73f212e9dc0b27dc93a7e584a8b Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 21 Jun 2021 18:14:26 -0700 Subject: [PATCH 4/7] Update windowsxamlmanager_initializeforcurrentthread_14911797.md --- .../windowsxamlmanager_initializeforcurrentthread_14911797.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index 3a06807a65..1092210b3f 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -29,7 +29,7 @@ This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that co m_xamlSource.Close(); m_xamlManager = nullptr; -// Drain the message queue after releasign WindowsXamlManager since rundown is async +// Drain the message queue after releasing WindowsXamlManager since rundown is async while (PeekMessageW(&msg, nullptr, 0, 0, PM_REMOVE)) { ::DispatchMessageW(&msg); From 756d2695ddafc30de455e2eac836d26c8f3dcbd6 Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 21 Jun 2021 18:19:07 -0700 Subject: [PATCH 5/7] Update windowsxamlmanager_initializeforcurrentthread_14911797.md --- .../windowsxamlmanager_initializeforcurrentthread_14911797.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index 1092210b3f..2835bd382f 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -21,7 +21,7 @@ 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). Call this method if your application creates the **Windows.UI.Xaml.UIElement** objects before it creates the **DesktopWindowXamlSource** object that will host them or your program uses more than one thread that uses Xaml. Call this method before creating the **DesktopWindowXamlSource**. -This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework. Call this method as many times as you want but generally get one instance and run it down after running down the **DesktopWindowXamlSource**. Note, after releasing this object a message loop must be run to finish the resource rundown, otherwise the assocaited resources will be leaked. +This method returns a [WindowsXamlManager](windowsxamlmanager.md) object that contains a reference to the UWP XAML framework. Call this method as many times as you want but generally get one instance and run it down after running down 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 From 7fc51494c28f5f2abf645b9d28bdd174b48a4be0 Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 1 Nov 2021 11:37:35 -0700 Subject: [PATCH 6/7] Update windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md Co-authored-by: Alexander Sklar --- .../windowsxamlmanager_initializeforcurrentthread_14911797.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index 2835bd382f..e1b571914c 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -19,7 +19,9 @@ 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). Call this method if your application creates the **Windows.UI.Xaml.UIElement** objects before it creates the **DesktopWindowXamlSource** object that will host them or your program uses more than one thread that uses Xaml. Call this method before creating the **DesktopWindowXamlSource**. +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. + +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. Call this method as many times as you want but generally get one instance and run it down after running down 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. From 8578a5aad308add7558d54e1d6bb639937dca6ef Mon Sep 17 00:00:00 2001 From: Chris Guzak Date: Mon, 1 Nov 2021 11:38:02 -0700 Subject: [PATCH 7/7] Update windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md Co-authored-by: Alexander Sklar --- .../windowsxamlmanager_initializeforcurrentthread_14911797.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md index e1b571914c..2d83026cb0 100644 --- a/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md +++ b/windows.ui.xaml.hosting/windowsxamlmanager_initializeforcurrentthread_14911797.md @@ -23,7 +23,8 @@ Call this method to initialize the internal UWP XAML framework for the current t 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. Call this method as many times as you want but generally get one instance and run it down after running down 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. +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