fatal signal 11 SIGSEGV when closing BluetoothSocket on Android 4.2.2 and 4.3

五迷三道 提交于 2019-12-04 10:08:29
markrileybot

I ran into this problem as well. However, for me close() was not the cause. The problem was accessing the socket after close(). Specifically, the call to available() on the socket's inputstream after the socket was closed caused the segfault.

I had the exact same problem. It helped to me before closing socket call

mConnectedThread.interrupt()

from outer class, and to ConnectedThread run() method add

public void run() {
    ...
    while (condition) {
        if (isInterrupted())
            return;
        ...
    }
    ...
}

to make sure thread doesn't read anything from socket's stream. After that you could call mmSocket.close()

Kushal

I faced same problem with Android 4.4.2 in Galaxy Grand 2 device

I fixed the problem with help of answer given by @markrileybot answer

I was accessing the socket.available() method after close() call is done by my code.

To add more to @markrileybot answer :

I had also added condition before calling socket.close() :

public void cancel() {
    try {
        if(mmSocket.isConnected())    // this is condition
        mmSocket.close();
        Utils.pause(1000);
    } catch (IOException e) {

        Log.e(TAG, "/S4B/ close() of connect socket failed", e);
    }
}

1 If you are sharing same socket between multiple threads, then this condition should be added which will prevent calling close() if socket is already closed (not connected)

2 Before calling socket.available() or socket.write(), we should check socket.isConnected() and if it returns true then only we should process further

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!