Skip to content

Fix unboxing and context leaks - #2664

Open
Manodasan Wignarajah (manodasanW) wants to merge 2 commits into
manodasanw/fix-imported-rcw-reference-trackingfrom
manodasanw/fix-unboxing-and-context-leaks
Open

Manodasan Wignarajah (manodasanW) wants to merge 2 commits into
manodasanw/fix-imported-rcw-reference-trackingfrom
manodasanw/fix-unboxing-and-context-leaks

Conversation

@manodasanW

Copy link
Copy Markdown
Member

Summary

Fix native resource leaks in context-aware object references and in boxed delegate, array, TypeName and non-blittable struct unboxing. Also fix generated boxed generic delegate callbacks, which reported TrackerObject instead of NonWrapping and caused ComWrappers to throw NotSupportedException.

Motivation

A lifetime review found several ownership defects in CsWinRT 3.0:

  • Context callbacks: each context-aware object reference acquires an owned COM context callback through CoGetObjectContext, which was never released. CsWinRT 2.x released it. 10,000 wrapper create/dispose cycles leaked exactly 10,000 references to the current context's object context, on both MTA and STA threads.
  • Delegate unboxing: this leaked the queried IReference<T> pointer and the delegate reference returned by get_Value.
  • Generic delegate boxes: generated callbacks for these returned the already-registered delegate with TrackerObject. ComWrappers then tried to register that same delegate as the wrapper for the box's separate COM identity, throwing NotSupportedException for native delegates.
  • Array unboxing: this never freed the array returned by IReferenceArray<T>.get_Value, including its owned elements. 50,000 unboxes of a 16-string array grew private memory by about 104 MiB.
  • TypeName unboxing: this never disposed the unboxed ABI value, leaking its HSTRING. Generated non-blittable struct unboxing has the same missing disposal; that was found by code inspection and not measured.

All of these were reproduced on .NET 10 and .NET 11, except where noted. None was introduced by the preceding reference-tracking fixes.

Changes

  • ContextAwareObjectReference.cs, WindowsRuntimeObjectReference.Lifecycle.cs:
    • Context-aware references now release their owned context callback exactly once, when their native resources are released. This also happens when releasing the wrapped object is suppressed (for example under COM aggregation).
    • Removed the unused constructors that accepted an existing callback and context token. These would otherwise have implicitly taken ownership of a caller-provided callback.
  • WindowsRuntimeDelegateMarshaller.cs: both delegate unboxing overloads now release the acquired IReference<T> pointer and the delegate returned by get_Value in finally.
  • InteropTypeDefinitionBuilder.Delegate.cs: generated boxed generic delegate callbacks now report NonWrapping, matching hand-written delegate and other boxed-value callbacks.
  • InteropTypeDefinitionBuilder.SzArray.cs, IWindowsRuntimeArrayComWrappersCallback.cs, WindowsRuntimeArrayMarshaller.cs:
    • Generated array callbacks now take ownership of the array returned by IReferenceArray<T>.get_Value and free it with the existing marshaller's Free in finally.
    • The ownership contract is now documented on IWindowsRuntimeArrayComWrappersCallback.
  • ABI/System/Type.cs, StructEnumMarshallerFactory.cs: TypeName and generated non-blittable struct unboxing now dispose the unboxed ABI value after conversion, matching existing return-value cleanup.
  • AgileReferenceTests.cs, BoxedValueUnboxingTests.cs: added regressions using fake native COM objects whose reference counts the tests can observe, run through the real product marshalling paths.
    • Context callback release, measured on a dedicated STA.
    • Generic delegate unboxing, covering both reference release and the wrapper flag.
    • Array element release through the real generated callback.

Validation

  • Full object lifetime suite: 37/37 on .NET 11 CoreCLR. Native AOT was last run before the final test-only cleanup.
  • Context callback and delegate unboxing regressions: both fail with the previous runtime, which leaked 100 context-object references and retained the box and delegate references.
  • Array and generic delegate regressions: generated WinRT.Interop.dll with the previous interop generator from the same captured inputs. The array regression fails with retained element references, and the generic delegate regression fails with NotSupportedException.
  • Generated code: inspection of the generated interop assembly confirmed that all generic delegate box callbacks now report NonWrapping, and that the regenerated struct marshallers dispose their unboxed values.
  • Memory measurements:
    • Repeated string array unboxing no longer grows private memory (from about 2.2 KB per call to about 0).
    • TypeName unboxing growth fell from about 121 bytes to about 4 bytes per call, which is measurement noise.

Release the context callback owned by context-aware object references when their native
resources are released, including when releasing the wrapped object is suppressed for
COM aggregation. Remove the unused constructors that accepted an existing callback.

Release the 'IReference<T>' pointer and the delegate returned by 'get_Value' when
unboxing delegates, and report 'NonWrapping' from generated boxed generic delegate
callbacks, as the unboxed delegate is not a wrapper for the box.

Free the array returned by 'IReferenceArray<T>.get_Value' in generated array callbacks,
and dispose unboxed 'TypeName' and non-blittable struct values after conversion.

Add regressions for context callback release and for delegate and array unboxing.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7a1f4f01-71f4-41e9-b3e5-ff0f20d758b1
Use fixed-address readonly vtables based on the runtime vtable types for the native
fixtures, reuse 'AsyncQueue.CollectUntilTrue' to wait for collection, and use
consistent named arguments in multiline calls.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
Copilot-Session: 7a1f4f01-71f4-41e9-b3e5-ff0f20d758b1
@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines:
There may be pipelines that require an authorized user to comment /azp run to run.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant