Exceptions in class AndroidSwitchableRouter

classic Classic list List threaded Threaded
5 messages Options
Reply | Threaded
Open this post in threaded view
|

Exceptions in class AndroidSwitchableRouter

Jose Luis Larraz
There are 2 exceptions that are caused:
1- when you are shuting down the service you get the exception that said "you have leaked an intent"
2-when you try to unregister the receiver you get one exception saying that this receiver is not registered
@Override
    public void shutdown() {
        super.shutdown();
        if (broadcastReceiver != null) {
            context.unregisterReceiver(broadcastReceiver);
        }
    }

My phone is one Nexus S with 4.1.2 os version.

I am trying to find a good solution to this but unfortunately by the moment my solution only works a few times not always.

Note: before someone said that is fault of my code, i have to say that this test has been made with the Browser demo and with the download jars (works really fine except when you quit the app, then you get this exceptions)

01-24 19:00:35.465: E/ActivityThread(3908): Service org.fourthline.cling.android.AndroidUpnpServiceImpl has leaked IntentReceiver org.fourthline.cling.android.AndroidSwitchableRouter$ConnectivityBroadcastReceiver@4146ba00 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-24 19:00:35.465: E/ActivityThread(3908): android.app.IntentReceiverLeaked: Service org.fourthline.cling.android.AndroidUpnpServiceImpl has leaked IntentReceiver org.fourthline.cling.android.AndroidSwitchableRouter$ConnectivityBroadcastReceiver@4146ba00 that was originally registered here. Are you missing a call to unregisterReceiver()?
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.LoadedApk$ReceiverDispatcher.<init>(LoadedApk.java:792)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.LoadedApk.getReceiverDispatcher(LoadedApk.java:593)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ContextImpl.registerReceiverInternal(ContextImpl.java:1111)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1098)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ContextImpl.registerReceiver(ContextImpl.java:1092)
01-24 19:00:35.465: E/ActivityThread(3908): at android.content.ContextWrapper.registerReceiver(ContextWrapper.java:365)
01-24 19:00:35.465: E/ActivityThread(3908): at org.fourthline.cling.android.AndroidSwitchableRouter.<init>(AndroidSwitchableRouter.java:69)
01-24 19:00:35.465: E/ActivityThread(3908): at org.fourthline.cling.android.AndroidUpnpServiceImpl.createRouter(AndroidUpnpServiceImpl.java:70)
01-24 19:00:35.465: E/ActivityThread(3908): at org.fourthline.cling.android.AndroidUpnpServiceImpl$1.createRouter(AndroidUpnpServiceImpl.java:54)
01-24 19:00:35.465: E/ActivityThread(3908): at org.fourthline.cling.UpnpServiceImpl.<init>(UpnpServiceImpl.java:81)
01-24 19:00:35.465: E/ActivityThread(3908): at org.fourthline.cling.android.AndroidUpnpServiceImpl$1.<init>(AndroidUpnpServiceImpl.java:51)
01-24 19:00:35.465: E/ActivityThread(3908): at org.fourthline.cling.android.AndroidUpnpServiceImpl.onCreate(AndroidUpnpServiceImpl.java:51)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ActivityThread.handleCreateService(ActivityThread.java:2363)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ActivityThread.access$1600(ActivityThread.java:130)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1277)
01-24 19:00:35.465: E/ActivityThread(3908): at android.os.Handler.dispatchMessage(Handler.java:99)
01-24 19:00:35.465: E/ActivityThread(3908): at android.os.Looper.loop(Looper.java:137)
01-24 19:00:35.465: E/ActivityThread(3908): at android.app.ActivityThread.main(ActivityThread.java:4745)
01-24 19:00:35.465: E/ActivityThread(3908): at java.lang.reflect.Method.invokeNative(Native Method)
01-24 19:00:35.465: E/ActivityThread(3908): at java.lang.reflect.Method.invoke(Method.java:511)
01-24 19:00:35.465: E/ActivityThread(3908): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
01-24 19:00:35.465: E/ActivityThread(3908): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-24 19:00:35.465: E/ActivityThread(3908): at dalvik.system.NativeStart.main(Native Method)
Reply | Threaded
Open this post in threaded view
|

Re: Exceptions in class AndroidSwitchableRouter

Draško
From my experience this kind of errors (no.2) are happening when different contexts are used (activity vs application) to register and unregister a broadcast receiver, or bind and unbind a service).

The error no.1 is a follow up, probably caused by the error no.2
Reply | Threaded
Open this post in threaded view
|

Re: Exceptions in class AndroidSwitchableRouter

Jose Luis Larraz
It seems that i have found a solution, i have modified ConnectivityBroadcastReceiver, in this way. And i works pretty fine, now in the shutwon you have to call to unregister().

class ConnectivityBroadcastReceiver extends BroadcastReceiver {

    private Context mContext;
    public ConnectivityBroadcastReceiver(Context context)
    {    
    mContext=context;
    try
    {
   
    mContext.getApplicationContext().registerReceiver(this, new IntentFilter("android.net.conn.CONNECTIVITY_CHANGE"));
    }
    catch(Exception e)
    {
    Log.e("public ConnectivityBroadcastReceiver(Context context) ", e.toString());    
    }
    }
   
    public void unregister()
    {
    try
    {
    mContext.getApplicationContext().unregisterReceiver(this);
    }
    catch(Exception e)
    {
    Log.e("unregisterCon() ", e.toString());    
    }
    }
Reply | Threaded
Open this post in threaded view
|

Re: Exceptions in class AndroidSwitchableRouter

Draško
yeap, that is a safe way ... register and unregister your broadcast receiver to the application context (an activity context vary depending in which activity you are, while there is only one application context)
Reply | Threaded
Open this post in threaded view
|

Re: Exceptions in class AndroidSwitchableRouter

Christian Bauer
Administrator
The correct solution is to properly shutdown the UpnpService when the last Activity unbinds from the AndroidUpnpService, in onDestroy(). Committed to master.