Repository navigation
Fix unboxing and context leaks - #2664
Open
Manodasan Wignarajah (manodasanW) wants to merge 2 commits into
Open
Manodasan Wignarajah (manodasanW) wants to merge 2 commits into
Manodasan Wignarajah (manodasanW) wants to merge 2 commits into
Conversation
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: There may be pipelines that require an authorized user to comment /azp run to run. |
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.
Summary
Fix native resource leaks in context-aware object references and in boxed delegate, array,
TypeNameand non-blittable struct unboxing. Also fix generated boxed generic delegate callbacks, which reportedTrackerObjectinstead ofNonWrappingand caused ComWrappers to throwNotSupportedException.Motivation
A lifetime review found several ownership defects in CsWinRT 3.0:
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.IReference<T>pointer and the delegate reference returned byget_Value.TrackerObject. ComWrappers then tried to register that same delegate as the wrapper for the box's separate COM identity, throwingNotSupportedExceptionfor native delegates.IReferenceArray<T>.get_Value, including its owned elements. 50,000 unboxes of a 16-string array grew private memory by about 104 MiB.TypeNameunboxing: 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:WindowsRuntimeDelegateMarshaller.cs: both delegate unboxing overloads now release the acquiredIReference<T>pointer and the delegate returned byget_Valueinfinally.InteropTypeDefinitionBuilder.Delegate.cs: generated boxed generic delegate callbacks now reportNonWrapping, matching hand-written delegate and other boxed-value callbacks.InteropTypeDefinitionBuilder.SzArray.cs,IWindowsRuntimeArrayComWrappersCallback.cs,WindowsRuntimeArrayMarshaller.cs:IReferenceArray<T>.get_Valueand free it with the existing marshaller'sFreeinfinally.IWindowsRuntimeArrayComWrappersCallback.ABI/System/Type.cs,StructEnumMarshallerFactory.cs:TypeNameand 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.Validation
WinRT.Interop.dllwith the previous interop generator from the same captured inputs. The array regression fails with retained element references, and the generic delegate regression fails withNotSupportedException.NonWrapping, and that the regenerated struct marshallers dispose their unboxed values.TypeNameunboxing growth fell from about 121 bytes to about 4 bytes per call, which is measurement noise.