Skip to content

Conversation

@Lindsor
Copy link
Owner

@Lindsor Lindsor commented Oct 28, 2025

Fix BroadcastReceiver memory leak in scanForWifi

Summary

Fixed a critical memory leak in the Android implementation where a BroadcastReceiver was registered in scanForWifi() but never unregistered. The receiver is now properly unregistered after scan results are processed in all code paths:

  • After successful scan results are received
  • After unsuccessful scan results
  • When startScan() returns false (fallback case)

Files changed:

  • android/src/main/java/com/lindsor/capacitor/wifi/Wifi.java - Added 3 lines to unregister the receiver

Review & Testing Checklist for Human

This is a YELLOW risk change - the fix is straightforward but I was unable to test on an actual Android device.

  • Test wifi scanning on a physical Android device - Verify scanWifi() still works correctly and returns results
  • Check for crashes - Monitor logcat for any IllegalArgumentException from attempting to unregister an already-unregistered receiver
  • Verify memory leak is fixed - Use Android Profiler to confirm the receiver is being garbage collected after scans
  • Test fallback path - Verify the case where wifiManager.startScan() returns false (may require specific device conditions or mocking)

Test Plan

  1. Call scanWifi() multiple times in succession (10-20 times)
  2. Use Android Profiler to monitor BroadcastReceiver instances - should not accumulate
  3. Check logcat for any unregister-related exceptions
  4. Verify scan results are still returned correctly

Notes

  • I was unable to run the full Android build (npm run verify:android) to verify compilation, only ran TypeScript build and prettier checks which passed
  • The fix follows Android best practices for BroadcastReceiver lifecycle management
  • This issue was identified as part of a comprehensive efficiency analysis (see EFFICIENCY_REPORT.md for other potential improvements)

Link to Devin run: https://app.devin.ai/sessions/7fcc006dadc142ac80efd3ab5b608a62
Requested by: Marcelo Luz (marcelo.luz@lindsor.com) (@Lindsor)

The BroadcastReceiver registered in scanForWifi was never unregistered,
causing a memory leak. This fix ensures the receiver is unregistered after
the scan completes in all code paths:
- After successful scan results are received
- After unsuccessful scan results
- When startScan() returns false (fallback case)

This prevents memory leaks, improves app stability, and reduces battery drain.

Co-Authored-By: Marcelo Luz <marcelo.luz@lindsor.com>
@devin-ai-integration
Copy link

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

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.

2 participants