Skip to content

TestComponent: add CountedFactory and FactoryCounter - #5

Merged
dlech merged 2 commits into
masterfrom
factory-reference-count
Nov 17, 2026
Merged

dlech merged 2 commits into
masterfrom
factory-reference-count

Conversation

@dlech

@dlech dlech commented Nov 17, 2026

Copy link
Copy Markdown
Member

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 CountedFactory activation factory counts itself (an atomic counter, incremented in the factory's constructor and decremented in its destructor). C++/WinRT's DllGetActivationFactory makes 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. FactoryCounter reads the count through a factory of its own, so reading it does not hold a CountedFactory factory.

Why

pywinrt's runtime keeps the agile activation factory of each constructor and static it calls. winrt.runtime.uninit_apartment() now releases them before CoUninitialize(), because the last CoUninitialize() 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, call uninit_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, then uninit_apartment(), then the static again in a new apartment:

    count after with the runtime fix without it
    nothing yet 0 0
    a static 1 1
    the static again 1 1
    the constructor 2 2
    uninit_apartment() 0 2
    a static in a new apartment 1 2
    uninit_apartment() again 0 2

    The 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.

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.
@dlech
dlech merged commit c28518a into master Nov 17, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant