Android muse.disconnect() bug 1.0.1


#1

muse.disconnect() isn’t working as I’d expect.
Here’s the code I’m testing with:

@Override
protected void onDestroy(){
    Log.d("onDestroy", "onDestroy");
    try{
        if (muse!=null&&muse.getConnectionState()!=ConnectionState.DISCONNECTED){
            Log.d("onDestroy", "unregisterAllListeners");
            muse.unregisterAllListeners();
            Log.d("onDestroy", "disconnecting");
            muse.disconnect(false);
            Log.d("onDestroy", "muse disconnected");
        } else {
            Log.d("onDestroy", "no muse connection to disconnect");
        }
    } catch(Exception e){}
    Log.d("onDestroy", "Final termination");
    //System.exit(0);//Proper death not just current activity
    super.onDestroy();
}

If you unregister the listeners manually and do disconnect(true) you get a seg fault.

muse.unregisterAllListeners();
muse.disconnect(true);

02-09 02:05:32.973: D/onDestroy(18309): onDestroy
02-09 02:05:32.973: D/onDestroy(18309): unregisterAllListeners
02-09 02:05:32.973: D/onDestroy(18309): disconnecting
02-09 02:05:32.973: D/onDestroy(18309): muse disconnected
02-09 02:05:32.973: A/libc(18309): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 18323 (Thread-72027)
02-09 02:05:32.973: D/onDestroy(18309): Final termination

Here I would not expect disconnect(true) to trigger a disconnect event. If this is as intended, please update the docs.

//Disabled muse.unregisterAllListeners();
muse.disconnect(true);

02-09 02:11:06.529: D/onDestroy(20479): onDestroy
02-09 02:11:06.529: D/onDestroy(20479): disconnecting
02-09 02:11:06.539: I/Muse connection(20479): Muse 00:06:66:68:96:21 CONNECTED -> DISCONNECTED
02-09 02:11:06.539: D/Muse connection(20479): Retrying connection

For the benefit of others reading this thread, this is how I recommend disconnecting in onDestroy for everything to work.

muse.unregisterAllListeners();
muse.disconnect(false);

02-09 02:12:27.346: D/onDestroy(21067): onDestroy
02-09 02:12:27.346: D/onDestroy(21067): unregisterAllListeners
02-09 02:12:27.346: D/onDestroy(21067): disconnecting
02-09 02:12:27.346: D/onDestroy(21067): muse disconnected
02-09 02:12:27.346: D/onDestroy(21067): Final termination


#2

Yikes, I’ve been trying to reproduce that segfault for a while! Thanks for the detailed info on it – that might help me track it down.

Also thanks for the feedback on disconnect() vs the disconnection event. Definitely worth taking into account.


#3
  1. disconnect(true) unregisters all listeners at the end: after closing bluetooth socket and sending connection event. Thanks for pointing at this, we’ve improved documentation. It will be accessible in next release.
  2. Segfault: can you please answer two questions: does it happen all the time or periodically? does it happen only onDestroy() or it may happen in any place with such code?

#4

I only disconnect in onDestroy(), so that’s the only place the segfault is happening for me. It’s consistent though. Every time I close my app with that code, it segfaults.


#5

I am now unable to re-create this seg fault with my latest code. Perhaps I still had a lib from the old version in there? v1.0.1 looks good! :slight_smile:


#6

I’m really sorry, but it just happened again!

02-16 00:02:36.219: D/onDestroy(21976): onDestroy
02-16 00:02:36.219: D/onDestroy(21976): unregisterAllListeners
02-16 00:02:36.219: D/onDestroy(21976): disconnecting
02-16 00:02:36.219: A/libc(21976): Fatal signal 11 (SIGSEGV) at 0x00000008 (code=1), thread 22206 (Thread-109820)
02-16 00:02:36.219: D/onDestroy(21976): muse disconnected
02-16 00:02:36.219: D/onDestroy(21976): Final termination
    protected void onDestroy(){
        Log.d("onDestroy", "onDestroy");
      
        //Delete all temp files
        for (int i=0;i<tempFilesToDelete.size();i++){
            deleteTempFile(tempFilesToDelete.get(i));
        }
      
        try{
            stopGraphAnimation();
            if (muse!=null&&muse.getConnectionState()!=ConnectionState.DISCONNECTED){
                Log.d("onDestroy", "unregisterAllListeners");
                muse.unregisterAllListeners();
                Log.d("onDestroy", "disconnecting");
                muse.disconnect(true);
                Log.d("onDestroy", "muse disconnected");
            } else {
                Log.d("onDestroy", "no muse connection to disconnect");
            }
        } catch(Exception e){}
        Log.d("onDestroy", "Final termination");
        super.onDestroy();
    }

Not sure what is making it happen. The same build just ran and exited fine a bunch of times after this happened.


#7

Okay! Any time it shows up, grab the raw, unfiltered logcat dump and attach it – the stack trace should be in there. I’ve seen one stack trace and it’s totally mysterious, so I want to see if it’s in the same place each time or in different places.