question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Effectively stopping updates and unsubscribing from ValueUpdated events

See original GitHub issue

To 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

  1. Connect to bluetooth device, start receiving values from a characteristic with StartUpdatesAsync() and App._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 with PushModalAsync

  2. Attempt to stop/unsubscribe from the event with App._characteristics[1].ValueUpdated -= ReceiveData and App._characteristics[1].StopUpdatesAsync();

  3. 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:closed
  • Created 6 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
Yelnikcommented, Apr 22, 2017

Ok I think it works now… Thanks

0reactions
xabrecommented, Apr 22, 2017

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 delegate ReceiveData. This does not work and causes your memory leak. You should subscribe directly with your delegate ValueUpdated += ReceiveData; otherways there is no way to unsubscribe an anonymous delgate (your lambda). 😃

Read more comments on GitHub >

github_iconTop Results From Across the Web

Effectively stopping updates and unsubscribing from ...
You are using a lanbda to wrap your method and subscribe to the value updated event ( ValueUpdated += (o, args) => {...
Read more >
How to subscribe to and unsubscribe from events - C# ...
In order to prevent resource leaks, you should unsubscribe from events before you dispose of a subscriber object.
Read more >
c# - Handling large amount of an event handlers
I have a control which can raise events. public event EventHandler ValueUpdated;. I subscribe to this event classInstance.ValueUpdated += ...
Read more >
c# - Should I unsubscribe from events?
In order to prevent resource leaks, it is important to unsubscribe from events before you dispose of a subscriber object.
Read more >
GitLab 16.1 released with all new navigation
Today, we are excited to announce the release of GitLab 16.1 with all new navigation, GitLab Dedicated General Availability, Kubernetes resource ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found