Ensure that EventEmitterWrapper is cleaned up as soon as a View is deleted

Summary:
In T92179998, there is a crash when an EventEmitterWrapper is deallocated after the JS VM is torn down, and the EventEmitterWrapper tries to free its JSI::Pointer reference (double-free).

To make sure this happens less/not at all, free EventEmitterWrappers on ViewState when a view is deleted, instead of waiting for all of RN to be torn down.

Changelog: [Internal]

Reviewed By: fkgozali

Differential Revision: D29020768

fbshipit-source-id: 9d72a23bc9966992ef56c1e3ee523405d4333194
This commit is contained in:
Joshua Gross
2021-06-09 23:48:04 -07:00
committed by Facebook GitHub Bot
parent 43cf565866
commit 25e8fbe8ff
@@ -813,6 +813,14 @@ public class SurfaceMountingManager {
viewState.mStateWrapper = null;
}
// Destroy EventEmitterWrapper immediately instead of waiting for Java GC.
// Notably, this is also required to ensure that the EventEmitterWrapper is deallocated
// before the JS VM is deallocated, since it holds onto a JSI::Pointer.
if (viewState.mEventEmitter != null) {
viewState.mEventEmitter.destroy();
viewState.mEventEmitter = null;
}
// For non-root views we notify viewmanager with {@link ViewManager#onDropInstance}
ViewManager viewManager = viewState.mViewManager;
if (!viewState.mIsRoot && viewManager != null) {