Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix InvalidCastExceptions from RCW reuse #833

Merged
merged 7 commits into from
May 4, 2021
Merged

Conversation

manodasanW
Copy link
Member

We were seeing InvalidCastExceptions because after the native pointer is released, we are still getting old RCW back from the .NET cache if that same pointer is encountered again due to .NET thinks it is still alive. This is due to the cache cleanup happens after finalization of the RCW which isn't at the same time as when the object reference in the RCW releases the final ref on the pointer. In .NET 6, there would be a new API for us to use to clear the cache, but until then we are putting in a mitigation for .NET 5.

IObjectReferences which are associated with an RCW are not immediately finalized but their finalization is deferred twice in an attempt to get its finalization to happen in gen 2 which means the RCW finalization and cache cleanup in gen 2 will be done by that time.

Fixes #762

@manodasanW manodasanW merged commit 3f5dd34 into master May 4, 2021
@manodasanW manodasanW deleted the manodasanw/rcwfix branch May 4, 2021 20:18
manodasanW added a commit that referenced this pull request May 11, 2021
manodasanW added a commit that referenced this pull request May 13, 2021
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.

Random System.InvalidCastException thrown when using Windows.Graphics.Imaging.BitmapDecoder and OcrEngine
3 participants