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

✨ Add possibility to cleanup resources when camera is being unmounted in frame processor plugin #3152

Open
2 tasks done
AlexShukel opened this issue Aug 24, 2024 · 5 comments
Labels
✨ feature Proposes a new feature or enhancement

Comments

@AlexShukel
Copy link

What feature or enhancement are you suggesting?

Currently, if frame processor plugin registers some listeners in it's constructor (for example, OrientationEventListener or any kind of sensors), there is no way to unregister those listeners when camera component unmounts.

The only way to unregister listeners is using onHostPause and onHostDestroy from LifecycleEventListener, but in that case listeners will drain battery even if user closed camera activity.

What Platforms whould this feature/enhancement affect?

iOS, Android

Alternatives/Workarounds

It is possible to register native module that will be able to cleanup resources, and call the native method from JS in useEffect when camera is being unmounted.

Additional information

@AlexShukel AlexShukel added the ✨ feature Proposes a new feature or enhancement label Aug 24, 2024
Copy link

Guten Tag, Hans here! 🍻

Ze request for cleaning up resources when mounting ze camera is a valid enhancement! It seems you’ve identified a potential issue with listeners draining battery after unmounting. Consider adding a cleanup method in your frame processor plugin to unregister listeners, as you mentioned in your alternatives.

If this needs further clarification or goes deeper than expected, feel free to open a discussion on our discussions page. If you want to support ze project and get quicker responses from mrousavy, consider sponsoring mrousavy.

Let’s see how ze community responds!

Note: If you think I made a mistake, please ping @mrousavy to take a look.

@mrousavy
Copy link
Owner

Hm, it's hard to add such lifecycle listeners because the Camera has no connection or knowledge of any of it's plugins.

You can add that code in the deinit/dealloc methods on iOS, and on finalize on Android, but it will only be called once the JS object (instance of the plugin) will actually be destroyed by the garbage collector..

@AlexShukel
Copy link
Author

@mrousavy I think it's okey to manually cleanup resources from react's useEffect hook in Camera component, but it is also not fully functional workaround.

The problem occurs when users leaves application in background - then it would be nice to cleanup all listeners in order to prevent battery drain. So, if you clean listeners in onHostPause method of com.facebook.react.bridge.LifecycleEventListener, then you should register them in onHostResume. But, you shouldn't register listeners on resume if camera is not mounted. However, it is not possible to determine whether camera is mounted or not from plugin side.

Maybe it is possible to add some kind of lifecycle indicator in VisionCameraProxy?

@mrousavy
Copy link
Owner

Good idea, yes we can add something like addLifecycleListener to VisionCameraProxy!

@mrousavy
Copy link
Owner

Also, Nitro Modules will have proper memory management and can be eagerly released - but that is still not public

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
✨ feature Proposes a new feature or enhancement
Projects
None yet
Development

No branches or pull requests

2 participants