Skip to content

Fix removeListener issue in WearMessagingClient and WearNodeDiscovery caused by SAM autoboxing#11

Open
MonteCreasor wants to merge 1 commit intophilipplackner:masterfrom
MonteCreasor:fix-remove-listener-bug
Open

Fix removeListener issue in WearMessagingClient and WearNodeDiscovery caused by SAM autoboxing#11
MonteCreasor wants to merge 1 commit intophilipplackner:masterfrom
MonteCreasor:fix-remove-listener-bug

Conversation

@MonteCreasor
Copy link

Summary

This PR fixes an issue in WearMessagingClient and WearNodeDiscovery where removeListener was not correctly removing the previously registered listener.

Issue

The problem occurs because Kotlin function types (EventType) -> Unit are not the same as the expected Java SAM interfaces (MessageClient.OnMessageReceivedListener and CapabilityClient.OnCapabilityChangedListener). When using a lambda directly, Kotlin boxes it into an anonymous class, causing removeListener to receive a different instance than addListener, which prevents proper removal.

Fix

  • Explicitly declare the listeners as SAM interfaces (OnMessageReceivedListener and OnCapabilityChangedListener) instead of using raw lambdas.
  • Ensure the exact same instance is passed to both addListener and removeListener.
  • Added unit tests to verify the issue and confirm the fix.

Temporary Changes

  • Added three testing libraries to run the new tests.
  • These should be removed before merging, as they were only needed for verification.

Impact

This fix ensures that listeners are properly removed, preventing memory leaks and unexpected event handling when the flow is closed.

Please review and let me know if any changes are needed.

Added two tests demonstrating that passing lambdas directly to MessageClient.addListener and CapabilityClient.addListener results in removeListener failing to remove them due to SAM autoboxing.

This commit also includes temporary test dependencies required to run these tests, which should be removed before merging.
@MonteCreasor MonteCreasor changed the title Added tests to verify removeListener failure due to SAM autoboxing Fix removeListener issue in WearMessagingClient and WearNodeDiscovery caused by SAM autoboxing Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant