darryncampbell / darryncampbell-cordova-plugin-intent

General purpose intent shim layer for cordova appliations on Android. Handles various techniques for sending and receiving intents.
MIT License
86 stars 130 forks source link

leaked IntentReceiver when closing app #100

Open OJ7 opened 4 years ago

OJ7 commented 4 years ago

In my app, I'm calling registerBroadcastReceiver (which works fine). However, I'm getting the following stack trace when closing the app (by swiping it away from the recent apps list).

com.example.app E/ActivityThread: Activity com.example.app.MainActivity has leaked IntentReceiver com.darryncampbell.cordova.plugin.intent.IntentShim$1@66e15a5 that was originally registered here. Are you missing a call to unregisterReceiver()?
    android.app.IntentReceiverLeaked: Activity com.example.app.MainActivity has leaked IntentReceiver com.darryncampbell.cordova.plugin.intent.IntentShim$1@66e15a5 that was originally registered here. Are you missing a call to unregisterReceiver()?
        at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1538)
        at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:1277)
        at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1555)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1528)
        at android.app.ContextImpl.registerReceiver(ContextImpl.java:1516)
        at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:636)
        at com.darryncampbell.cordova.plugin.intent.IntentShim.execute(IntentShim.java:172)
        at org.apache.cordova.CordovaPlugin.execute(CordovaPlugin.java:98)
        at org.apache.cordova.PluginManager.exec(PluginManager.java:132)
        at org.apache.cordova.CordovaBridge.jsExec(CordovaBridge.java:59)
        at org.apache.cordova.engine.SystemExposedJsApi.exec(SystemExposedJsApi.java:41)
        at android.os.MessageQueue.nativePollOnce(Native Method)
        at android.os.MessageQueue.next(MessageQueue.java:326)
        at android.os.Looper.loop(Looper.java:181)
        at android.os.HandlerThread.run(HandlerThread.java:65)

I noticed that the plugin does not have an onDestroy method. I am able to get rid of the above log by adding the following to IntentShim.java

    @Override
    public void onDestroy() {
        Log.d(LOG_TAG, "Destroying plugin");
        this.cordova.getActivity().unregisterReceiver(myBroadcastReceiver);
        super.onDestroy();
    }
darryncampbell commented 4 years ago

Thanks, marked as todo - check if receiver is registered and if so, unregister during onDestroy()