Fix dangling surfaces in ReactHostImpl (#44393)

Summary:
Though the `ReactHost.destroy()` is not being used from OSS code, we use it at Expo for expo-dev-client to change loading apps from different dev servers. Without cleanup the `mAttachedSurfaces`, it will have dangling or duplicated attached surfaces that cause duplicated react trees.

<img src="https://github.com/facebook/react-native/assets/46429/f84d274e-aaad-4352-9e3c-6262571a5625">

This PR tries to cleanup the `mAttachedSurfaces` from destroying.

## Changelog:

[ANDROID] [FIXED] - Fixed dangling `mAttachedSurfaces` after `ReactHost.destroy()`

Pull Request resolved: https://github.com/facebook/react-native/pull/44393

Test Plan: have to manually call `ReactHost.destroy()` and recreate the MainActivity without killing the process. then reload the app will startSurface for the same attached surfaces.

Reviewed By: RSNara

Differential Revision: D56901863

Pulled By: javache

fbshipit-source-id: c7f822501d971810ac6aa7262b15da69ec41355e
This commit is contained in:
Kudo Chien
2024-05-13 04:04:07 -07:00
committed by Facebook GitHub Bot
parent 596e14e423
commit c70d791036
@@ -1543,9 +1543,9 @@ public class ReactHostImpl implements ReactHost {
// Step 3: Stop all React Native surfaces
stopAttachedSurfaces(method, reactInstance);
// TODO(T161461674): Should we clear mAttachedSurfaces?
// Not clearing mAttachedSurfaces could lead to a memory leak.
synchronized (mAttachedSurfaces) {
mAttachedSurfaces.clear();
}
return task;
},