Skip to content

Commit

Permalink
Fix Memory Leak in LogBoxModule (#45261)
Browse files Browse the repository at this point in the history
Summary:
To fix [The Memory Leak Issue](#45080) This change modifies the timing of view creation in the LogModule. The motivation behind this update is to address a potential memory leak issue. Previously, views were being created and held onto, which could lead to references to the Activity being retained even when they were no longer needed. By creating the view only when the show method is called and ensuring it is removed in the hide method, we can prevent these memory leaks and improve the overall memory management and stability of the LogModule.

Fixes #45080

- Adjusted the timing of view creation to occur when the `show` method is called.
- Ensured that the created view can be removed in the `hide` method.
- This update addresses potential memory leaks by preventing the view from holding a reference to the Activity.

These changes improve memory management and stability within the LogModule.

Modify the timing of view creation in LogModule. The view is now created when the show method is called, and it can be removed in the hide method. This change resolves potential memory leaks caused by the view holding a reference to the Activity.

## Changelog:

[ANDROID] [FIXED] - Fix LogModule to create view when show is called

Pull Request resolved: #45261

Reviewed By: dmytrorykun

Differential Revision: D59372962

Pulled By: cortinico

fbshipit-source-id: 6693afdb279c7164ff0f68c93f8ca8a54b1c2077
  • Loading branch information
fannnzhang authored and facebook-github-bot committed Jul 5, 2024
1 parent b8ab0fe commit 0847384
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ internal class LogBoxDialogSurfaceDelegate(private val devSupportManager: DevSup
}

override fun hide() {
if (!isShowing) {
return
if (isShowing) {
dialog?.dismiss()
}
(reactRootView?.parent as ViewGroup?)?.removeView(reactRootView)
dialog?.dismiss()
dialog = null
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,20 +23,18 @@ public class LogBoxModule(
devSupportManager.createSurfaceDelegate(NAME)
?: LogBoxDialogSurfaceDelegate(devSupportManager)

/**
* LogBoxModule can be rendered in different surface. By default, it will use LogBoxDialog to wrap
* the content of logs. In other platform (for example VR), a surfaceDelegate can be provided so
* that the content can be wrapped in custom surface.
*/
init {
UiThreadUtil.runOnUiThread { surfaceDelegate.createContentView("LogBox") }
}

override fun show() {
if (!surfaceDelegate.isContentViewReady) {
return
UiThreadUtil.runOnUiThread {
if (!surfaceDelegate.isContentViewReady) {
/**
* LogBoxModule can be rendered in different surface. By default, it will use LogBoxDialog
* to wrap the content of logs. In other platform (for example VR), a surfaceDelegate can be
* provided so that the content can be wrapped in custom surface.
*/
surfaceDelegate.createContentView("LogBox")
}
surfaceDelegate.show()
}
UiThreadUtil.runOnUiThread { surfaceDelegate.show() }
}

override fun hide() {
Expand Down

0 comments on commit 0847384

Please sign in to comment.