Check if instance is valid before calling C++

Summary:
Changelog: [internal]

Why does the crash happen?
We call method on invalid address. We crash on `std::weak_ptr::lock` because that's the first accessed ivar.
In D29020768 (https://github.com/facebook/react-native/commit/25e8fbe8ffa1c91522e91819a781434093f8b759), eventEmitter may be destroyed before an event is dispatched. Calling destroy on hybrid object destroys only C++ part.

Reviewed By: JoshuaGross

Differential Revision: D29194906

fbshipit-source-id: ae8d9d90aa8d98d69d29884e80d6b930b1e66870
This commit is contained in:
Samuel Susla
2021-06-17 10:01:22 -07:00
committed by Facebook GitHub Bot
parent 30ce8618b9
commit a9f1e23d38
@@ -48,6 +48,9 @@ public class EventEmitterWrapper {
* @param params {@link WritableMap} payload of the event
*/
public void invoke(@NonNull String eventName, @Nullable WritableMap params) {
if (!isValid()) {
return;
}
NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params;
invokeEvent(eventName, payload);
}
@@ -61,6 +64,9 @@ public class EventEmitterWrapper {
*/
public void invokeUnique(
@NonNull String eventName, @Nullable WritableMap params, int customCoalesceKey) {
if (!isValid()) {
return;
}
NativeMap payload = params == null ? new WritableNativeMap() : (NativeMap) params;
invokeUniqueEvent(eventName, payload, customCoalesceKey);
}
@@ -70,4 +76,11 @@ public class EventEmitterWrapper {
mHybridData.resetNative();
}
}
private boolean isValid() {
if (mHybridData != null) {
return mHybridData.isValid();
}
return false;
}
}