From 848b02092a3247c7f38d9d59d6be4c053e625163 Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 17 Nov 2026 14:38:25 -0600 Subject: [PATCH 1/2] TestComponent: add CountedFactory and FactoryCounter CountedFactory's activation factories count themselves, and FactoryCounter reads the count through a factory of its own, so reading it holds none of them. DllGetActivationFactory makes a new factory each time it is asked for one and the component keeps none, so the count is how many factories its callers hold. This allows testing that a projection lets go of the activation factories it keeps, for example before it uninitializes COM, without waiting for the DLL to be unloaded under a factory it kept. --- TestComponent/CountedFactory.cpp | 33 +++++++++++++++++++++ TestComponent/CountedFactory.h | 24 +++++++++++++++ TestComponent/FactoryCounter.cpp | 12 ++++++++ TestComponent/FactoryCounter.h | 19 ++++++++++++ TestComponent/TestComponent.idl | 24 +++++++++++++++ TestComponent/TestComponent.vcxproj | 4 +++ TestComponent/TestComponent.vcxproj.filters | 4 +++ 7 files changed, 120 insertions(+) create mode 100644 TestComponent/CountedFactory.cpp create mode 100644 TestComponent/CountedFactory.h create mode 100644 TestComponent/FactoryCounter.cpp create mode 100644 TestComponent/FactoryCounter.h diff --git a/TestComponent/CountedFactory.cpp b/TestComponent/CountedFactory.cpp new file mode 100644 index 0000000..054f3a5 --- /dev/null +++ b/TestComponent/CountedFactory.cpp @@ -0,0 +1,33 @@ +#include "pch.h" +#include "CountedFactory.h" +#include "CountedFactory.g.cpp" + +#include + +namespace winrt::TestComponent::implementation +{ + void CountedFactory::StaticMethod() + { + } +} + +namespace winrt::TestComponent::factory_implementation +{ + // Factories are made and released on whatever thread asks for them. + static std::atomic s_count; + + CountedFactory::CountedFactory() noexcept + { + ++s_count; + } + + CountedFactory::~CountedFactory() + { + --s_count; + } + + uint32_t CountedFactory::Count() noexcept + { + return s_count; + } +} diff --git a/TestComponent/CountedFactory.h b/TestComponent/CountedFactory.h new file mode 100644 index 0000000..29076d8 --- /dev/null +++ b/TestComponent/CountedFactory.h @@ -0,0 +1,24 @@ +#pragma once +#include "CountedFactory.g.h" + +namespace winrt::TestComponent::implementation +{ + struct CountedFactory : CountedFactoryT + { + CountedFactory() = default; + + static void StaticMethod(); + }; +} + +namespace winrt::TestComponent::factory_implementation +{ + struct CountedFactory : CountedFactoryT + { + CountedFactory() noexcept; + ~CountedFactory(); + + // How many of these exist. + static uint32_t Count() noexcept; + }; +} diff --git a/TestComponent/FactoryCounter.cpp b/TestComponent/FactoryCounter.cpp new file mode 100644 index 0000000..95a81c5 --- /dev/null +++ b/TestComponent/FactoryCounter.cpp @@ -0,0 +1,12 @@ +#include "pch.h" +#include "FactoryCounter.h" +#include "FactoryCounter.g.cpp" +#include "CountedFactory.h" + +namespace winrt::TestComponent::implementation +{ + uint32_t FactoryCounter::CountedFactoryCount() + { + return factory_implementation::CountedFactory::Count(); + } +} diff --git a/TestComponent/FactoryCounter.h b/TestComponent/FactoryCounter.h new file mode 100644 index 0000000..6212c56 --- /dev/null +++ b/TestComponent/FactoryCounter.h @@ -0,0 +1,19 @@ +#pragma once +#include "FactoryCounter.g.h" + +namespace winrt::TestComponent::implementation +{ + struct FactoryCounter + { + FactoryCounter() = default; + + static uint32_t CountedFactoryCount(); + }; +} + +namespace winrt::TestComponent::factory_implementation +{ + struct FactoryCounter : FactoryCounterT + { + }; +} diff --git a/TestComponent/TestComponent.idl b/TestComponent/TestComponent.idl index 6eebb75..16232ec 100644 --- a/TestComponent/TestComponent.idl +++ b/TestComponent/TestComponent.idl @@ -447,4 +447,28 @@ namespace TestComponent void Cancel(); void InvokeCompletedHandler(); } + + // A class whose activation factories count themselves, for testing that a + // projection lets go of the activation factories it keeps. + // + // DllGetActivationFactory makes a new factory each time it is asked for + // one, and the component keeps none, so the count is how many factories + // its callers hold: a projection that keeps the factory it calls a + // constructor or a static through holds one for each factory it asked + // for, and none once it has let go of them. + [default_interface] + runtimeclass CountedFactory + { + CountedFactory(); + + static void StaticMethod(); + } + + // Reads the count of CountedFactory's activation factories through a + // factory of its own, so that reading it holds none of them. + static runtimeclass FactoryCounter + { + // How many activation factories of CountedFactory exist. + static UInt32 CountedFactoryCount{ get; }; + } } diff --git a/TestComponent/TestComponent.vcxproj b/TestComponent/TestComponent.vcxproj index 74c6c97..1df9352 100644 --- a/TestComponent/TestComponent.vcxproj +++ b/TestComponent/TestComponent.vcxproj @@ -152,8 +152,10 @@ + + @@ -165,8 +167,10 @@ + + diff --git a/TestComponent/TestComponent.vcxproj.filters b/TestComponent/TestComponent.vcxproj.filters index db02db5..10599f8 100644 --- a/TestComponent/TestComponent.vcxproj.filters +++ b/TestComponent/TestComponent.vcxproj.filters @@ -22,6 +22,8 @@ + + @@ -36,6 +38,8 @@ + + From 9776166c2c2b89e2622b125e2794d8be7f950c5d Mon Sep 17 00:00:00 2001 From: David Lechner Date: Tue, 17 Nov 2026 14:38:25 -0600 Subject: [PATCH 2/2] v1.7.0 --- TestComponent/TestComponent.nuspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/TestComponent/TestComponent.nuspec b/TestComponent/TestComponent.nuspec index d59294f..128ce60 100644 --- a/TestComponent/TestComponent.nuspec +++ b/TestComponent/TestComponent.nuspec @@ -2,7 +2,7 @@ PyWinRT.TestWinRT - 1.6.1 + 1.7.0 TestWinRT Microsoft MIT