Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Activity has leaked IntentReceiver #28

Closed
carstenbaumhoegger opened this issue Oct 5, 2015 · 6 comments
Closed

Activity has leaked IntentReceiver #28

carstenbaumhoegger opened this issue Oct 5, 2015 · 6 comments

Comments

@carstenbaumhoegger
Copy link

I'm using ReactiveNetwork in a normal Java class and pass an Activity as the Context.
Before the Activity gets destroyed I call mSubscription.unsubscribe, but the error still occurs.
Maybe you can have a look at it?

10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread: Activity has leaked IntentReceiver com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1$1@380ab58f that was originally registered here. Are you missing a call to unregisterReceiver()?
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread: android.app.IntentReceiverLeaked: Activity net.exesystem.carbikeobd.activity.Main has leaked IntentReceiver com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1$1@380ab58f that was originally registered here. Are you missing a call to unregisterReceiver()?
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:1003)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:767)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1749)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1729)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.app.ContextImpl.registerReceiver(ContextImpl.java:1723)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:488)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1.call(ReactiveNetwork.java:87)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at com.github.pwittchen.reactivenetwork.library.ReactiveNetwork$1.call(ReactiveNetwork.java:70)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:162)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:154)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:162)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable$2.call(Observable.java:154)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.Observable.unsafeSubscribe(Observable.java:7710)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.internal.operators.OperatorSubscribeOn$1$1.call(OperatorSubscribeOn.java:62)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at rx.internal.schedulers.ScheduledAction.run(ScheduledAction.java:55)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:422)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:152)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:265)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-05 23:49:18.350 22518-22518/net.exesystem.carbikeobd.debug E/ActivityThread:     at java.lang.Thread.run(Thread.java:818)
@pwittchen
Copy link
Owner

Can you provide code, which caused this error? It will be much more easier to reproduce this bug and fix it. Without that, it's quite hard.

In your error, we can see message Are you missing a call to unregisterReceiver()?, but receiver is unregistered in unsubscribe() method. Probably something is messed with Context or threads used in RxJava subscription.

Where do you call unsubscribe() method? It should be called in onPause() method in your Activity. If you call it somewhere else or don't call it at all, this error may occur.

@carstenbaumhoegger
Copy link
Author

Sure, here's the code, I'm using, hope this helps.

This is the method that does the subscription

    private void checkWifiConnection() {
        mWifiSubscription = new ReactiveNetwork().observeConnectivity(mActivity)
                .observeOn(Schedulers.io())
                .subscribeOn(Schedulers.io())
                .subscribe(connectivityStatus -> {
                    if(connectivityStatus == ConnectivityStatus.WIFI_CONNECTED) {
                        Log.w(TAG, "wifi connected!");
                        hasWifi = true;
                        if(!isConnected) {
                            establishConnection();
                        }
                    } else {
                        Log.w(TAG, "wifi not connected! " + connectivityStatus);
                        hasWifi = false;
                    }
                });
    }

And here I call unsubscribe

    public void closeConnection() {
        mWifiSubscription.unsubscribe();
        mActivity.finish();
    }

@pwittchen
Copy link
Owner

Thanks!
I have a few more questions. Where do you call checkWifiConnection() method and closeConnection() method? You've written in the first post that these methods are not located in the Activity. How do you call these methods from your Activity? I need to know more details to reproduce this bug. It may be incorrect library usage. I'm asking all these questions to clarify this issue.

@carstenbaumhoegger
Copy link
Author

checkWifiConnection() get's called in the Activities onResume() method and closeConnection() is called in onPause().
Could be wrong lib usage, but I don't know, what mistake I'm making here :D

@carstenbaumhoegger
Copy link
Author

Okay, I rewrote a few parts of the code and re-located the method calls and now it seems to work without the error.
My fault, sorry that I bothered you with it.
Great lib, keep up the good work!

@pwittchen
Copy link
Owner

Great! I'm glad you found source of the problem and fixed it. :-)

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

No branches or pull requests

2 participants