Repository navigation
TestComponent: add CountedFactory and FactoryCounter - #5
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds two classes for testing that a projection lets go of the activation factories it keeps:
[default_interface] runtimeclass CountedFactory { CountedFactory(); static void StaticMethod(); } static runtimeclass FactoryCounter { // How many activation factories of CountedFactory exist. static UInt32 CountedFactoryCount{ get; }; }Each
CountedFactoryactivation factory counts itself (an atomic counter, incremented in the factory's constructor and decremented in its destructor). C++/WinRT'sDllGetActivationFactorymakes a new factory each time it is asked for one and the component keeps none, so the count is how many factories the caller holds.FactoryCounterreads the count through a factory of its own, so reading it does not hold aCountedFactoryfactory.Why
pywinrt's runtime keeps the agile activation factory of each constructor and static it calls.
winrt.runtime.uninit_apartment()now releases them beforeCoUninitialize(), because the lastCoUninitialize()of the process unloads the DLLs that implement the classes, and a factory kept past it pointed into unmapped code and crashed the process. The pywinrt suite cannot reach that crash, since its multithreaded apartment stays for the whole process. With these classes a test can see the release itself, in a few milliseconds and without a crash: call a constructor or a static, see the count go up, calluninit_apartment(), see it drop back to 0, and call again to see it kept again.Verified
Built TestComponent for x64, x86 and ARM64 Release locally (Visual Studio 2026, v145 toolset, Windows SDK 10.0.26100), ran the Console self test, and packed a local nupkg.
Generated pywinrt's test-winrt projection from the new
TestComponent.winmd(no new ABI call shapes) and ran a script that calls the static twice and the constructor once in an STA, thenuninit_apartment(), then the static again in a new apartment:uninit_apartment()uninit_apartment()againThe run with the fix was done under both a debug (
python_d -X dev) and a release build, about 10 ms each.pywinrt's TestComponent, implements and delegates tests pass against the new DLL.
Merging publishes 1.7.0 to nuget.org.