Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Conversation

@github-actions
Copy link
Contributor

Backport of #21365 to 2020-02

/cc @lambdageek

The CCW methods for IUnknown (and in principle IDispatch - except they all have
trivial bodies) are native C code in the runtime that may allocate coop
handles.  Add a coop handle frame around the entire call in order to make sure
they're cleaned up and don't retain a reference.

This helps fix managed object leaks with code like:

```
   var o  = new SomeClass();
   var pUnk = Marshal.GetIUnknownForObject(o);
   int c = Marshal.Release(pUnk);
   o = null;
```

Which retains a reference to the `SomeClass` instance that won't be collected
until the thread dies, despite cleaning up the IUnknown refcount. The underling
ccw addref/release methods leak coop handles on the thread.

This is not an issue when the CCW calls some managed method because there are
no coop handles there until some icall (at which point it will set up the
coop handle stack properly).
@lambdageek

This comment has been minimized.

@github-actions

This comment has been minimized.

@github-actions

This comment has been minimized.

@lambdageek
Copy link
Member

@monojenkins build

@lambdageek
Copy link
Member

@monojenkins build failed

@lambdageek lambdageek merged commit b8d7525 into 2020-02 Dec 13, 2021
@akoeplinger akoeplinger deleted the backport/pr-21365-to-2020-02 branch January 27, 2022 15:07
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.

3 participants