react-native-ble-plx
                                
                                 react-native-ble-plx copied to clipboard
                                
                                    react-native-ble-plx copied to clipboard
                            
                            
                            
                        Potential memory leaks on multiple calls to BleManager constructor
Each time BleManager is created the createClient() function is called, which creates a new instance of the MBA/native clients for interacting with OS BT stack. This method does not check whether a client is already existing, it just creates a new one, always. Additionally it diminishes the benefit of having the state live in the native code which is not reloaded, because the application loses access to it.
One case where the leak might occur is full JS reload (as opposed to hot reload) while debugging.
I think of two plausible scenarios for finding it. The base is to start an operation that is emitting events, as all clients will use the same instance of the module to transmit those.
Prerequisite:
- Create BleManager instance.
- Connect to a device and run discovery
- Setup notifications on a characteristic X
Scenario 1 - reloading:
- Modify the library to log all notifications (before applying filtering to listeners)
- prerequisite with characteristic A
- Reload app from debug menu
- prerequisite with characteristic B
Scenario 2 - creating multiple managers:
- Modify the library to log all notifications (before applying filtering to listeners)
- prerequisite with characteristic A
- prerequisite with characteristic B
We should receive notifications from both characteristic, even though the client corresponding to characteristic A is no longer accessible to JS.
If the leak does occur, we can resolve it by either destroying the old client after receiving the call to create a new one OR ignoring the call if a valid client exists. In case we decided on the first solution, we should clean up any ongoing operations, which might be harder.
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.
I'm excited to inform you that we've released a new version of react-native-ble-plx. Many of the concerns and bugs have been addressed in this update.
If you encounter any issues or have further suggestions, please don't hesitate to open a new issue.