Effectively stopping updates and unsubscribing from ValueUpdated events
See original GitHub issueTo help us fix your issue, please provide the information in the below template. If something causes a crash, provide as much information as you can gather. Just imagine: we do not know what you are doing!
Note: There is often little we can do without a minimal reproducible sample of the issue, so please provide that in a standalone git repository and link it here.
Steps to reproduce
-
Connect to bluetooth device, start receiving values from a characteristic with
StartUpdatesAsync()
andApp._characteristics[1].ValueUpdated += (o, args) => { ReceiveData(o, args); };
(The device sends data once per 500 ms) The function that receives the data is on a new page that is loaded withPushModalAsync
-
Attempt to stop/unsubscribe from the event with
App._characteristics[1].ValueUpdated -= ReceiveData
andApp._characteristics[1].StopUpdatesAsync();
-
Reload the page with
PushModalAsync
and resubscribe to the event exactly the same as in step 1. The previous subscription will still be running, and has not terminated the previous page properly or released the previous event subscription. Data will be received twice every 500 ms (both threads running)
However, Xamarin does not think this other page is running, the Navigation stack does not see any modal pages, so the thread running the previous code is “who knows where”
Expected behavior
I expect to be unsubscribed from the updates and the thread associated with that subscription to be released
Actual behavior
The modal page or thread handling the data receiving is still running somewhere in the app. It appears the subscription to the data being received was never terminated properly
Crashlog
If something causes an exception paste full stack trace + Exception here
Configuration
Version of the Plugin: 1.3.0-alpha1
Platform: e.g. iOS 10.1 / Android 4.4 / … (including version!!! e.g. Android 5.1 / i0S 10) iOS: 10.3.1 Android: 7.0
Device: e.g. HTC Sensation /i Phone 7 … Samsung Galaxy S7 Edge iPhone 5C
Issue Analytics
- State:
- Created 6 years ago
- Comments:6
Top GitHub Comments
Ok I think it works now… Thanks
You are using a lanbda to wrap your method and subscribe to the value updated event (
ValueUpdated += (o, args) => { ReceiveData(o, args); };
) and then unsbuscribing the actual function delegateReceiveData
. This does not work and causes your memory leak. You should subscribe directly with your delegateValueUpdated += ReceiveData;
otherways there is no way to unsubscribe an anonymous delgate (your lambda). 😃