Skip to content

Fix COM ref counting in SOSMethodEnum#5749

Merged
hoyosjs merged 1 commit intodotnet:mainfrom
max-charlamb:com-ref-counting-fix
Mar 6, 2026
Merged

Fix COM ref counting in SOSMethodEnum#5749
hoyosjs merged 1 commit intodotnet:mainfrom
max-charlamb:com-ref-counting-fix

Conversation

@max-charlamb
Copy link
Member

Summary

Matching PR for dotnet/runtime#125231, which fixes DefaultCOMImpl::Release() reference counting bugs in the DAC.

Changes

1. Fix SOSMethodEnum simulator COM ref counting (util.cpp)

The SOSDacInterface15 simulator's GetMethodTableSlotEnumerator had the same bug pattern fixed in the runtime PR — raw pointer assignment without QueryInterface, and the null check occurred after the dereference. Fixed to:

  • Initialize refCount to 0 (matching DefaultCOMImpl convention)
  • Use QueryInterface to return the interface, properly calling AddRef
  • Check for null before dereferencing the out parameter
  • Use delete on failure (since QueryInterface wasn't called at refCount 0)

2. Fix ISOSMethodEnum leak in strike.cpp

Changed raw ISOSMethodEnum* to ToRelease<ISOSMethodEnum> so Release() is called when the pointer goes out of scope, matching the pattern used everywhere else (e.g., ToRelease<ISOSHandleEnum>).

- Fix SOSMethodEnum simulator to use standard COM ref counting:
  initialize refCount to 0 and use QueryInterface to return the
  interface pointer with a proper AddRef.
- Fix ISOSMethodEnum leak in DumpMT by using ToRelease<>.

Matching PR: dotnet/runtime#125231

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@max-charlamb max-charlamb requested a review from a team as a code owner March 6, 2026 16:18
Copilot AI review requested due to automatic review settings March 6, 2026 16:18
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR fixes two COM ref-counting bugs in the SOSDacInterface15 simulator, matching a parallel fix in the .NET runtime (dotnet/runtime#125231). The SOSMethodEnum class previously initialized its reference count to 1 (causing a count mismatch) and assigned the raw pointer directly to the output parameter without going through QueryInterface/AddRef. The failure-path cleanup also incorrectly called Release() when refCount was 1 (which would delete the object before returning the pointer), and in the strike.cpp call site the enumerator was never released.

Changes:

  • util.cpp: Fix SOSMethodEnum to initialize refCount to 0, use QueryInterface to properly AddRef on success, and use delete directly on the failure path (since QueryInterface was never called, refCount is still 0 and Release() would unsigned-underflow).
  • strike.cpp: Wrap ISOSMethodEnum* in ToRelease<> to ensure Release() is called on scope exit, fixing a resource leak.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
src/SOS/Strike/util.cpp Fixes COM ref-counting in SOSMethodEnum: initializes refCount to 0, routes output through QueryInterface, and uses delete in error cleanup.
src/SOS/Strike/strike.cpp Wraps ISOSMethodEnum* in ToRelease<> to prevent resource leak.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@max-charlamb max-charlamb changed the title Fix COM ref counting in SOSMethodEnum simulator Fix COM ref counting in SOSMethodEnum Mar 6, 2026
@hoyosjs hoyosjs merged commit 3959d5c into dotnet:main Mar 6, 2026
20 of 22 checks passed
table.WriteRow("Entry", "MethodDesc", "JIT", "Slot", "Name");

ISOSMethodEnum *pMethodEnumerator;
ToRelease<ISOSMethodEnum> pMethodEnumerator;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's going to happen when latest SOS with this change runs against older DAC? Is it going to just leak (that's ok) or crash (that's bad)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is unrelated to the changes in dotnet/runtime#125231 . This was a standard leak where the COM interface was never freed. It should not cause a problem with either DAC version

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.

4 participants