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

Escape mock during method dispatch on mock to avoid premature garbage collection. #2034

Merged
merged 1 commit into from Sep 3, 2020

Conversation

raphw
Copy link
Member

@raphw raphw commented Sep 3, 2020

Under heavy optimization mocks might get garbage collected during the dispatching of a mocked method if the mock instance is not used after this method dispatch. To avoid this, we escape the mock instance during the dispatch to make sure that the GC cannot collect the object.

Fixes #1802.

Copy link
Contributor

@TimvdLippe TimvdLippe left a comment

Choose a reason for hiding this comment

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

Seems like CheckStyle requires a license header. Other than that, nice fix! I like the extensive documentation as well 😄

@raphw raphw force-pushed the explicit-escape-during-dispatch branch from 03afdae to 7f73641 Compare September 3, 2020 21:40
@raphw
Copy link
Member Author

raphw commented Sep 3, 2020

Indeed, force pushed a new version just now.

Let's hope the JIT stays dumb enough to avoid this error in the future - I was really certain that the stack allocation of the mock object would avoid the allocation but if the entire Mockito call stack gets inlined, that does no longer apply. Worst case, we need to make the Mockito call stack deeper then the inlining heuristic ;)

@raphw raphw force-pushed the explicit-escape-during-dispatch branch from 7f73641 to 253559d Compare September 3, 2020 22:03
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.

Exception "The mock object was garbage collected."
2 participants