Java threading issue with handler message data being overwritten by next message

一曲冷凌霜 提交于 2019-12-06 14:07:39

Is it possible that you have to use a second byte buffer when creating the message object?

mHandler.obtainMessage(MESSAGE_READ, bytes, -1, copyOfBuffer)

I have a suspicion that mHandler (although I don't know what it is) is keeping a reference to that byte array you send him.

I realize this is an OLD thread, but for posterity...

I just ran across this same issue. Coincidentally, it was also in code based on the google Bluetooth chat sample. My code also talks to a bluetooth device whose data appears in 'snippets' in the mmInStream.read(), resulting in many small messages sent to the handler.

Like the original poster, I found that the messages were being overwritten. I believe this is due to the android implementation of message handling as a light weight messaging mechanism. Specifically, .obtainMessage() allocates from a global pool of message structures to avoid runtime allocations. In keeping with this requirement, they DO NOT COPY THE DATA (/OBJECT) contained within the message, but simply maintain a pointer to the original data. Each message object contains the count of bytes in msg.arg1. In the sample code, if the data within the byte array ('buffer') is changed before the receiving handler has processed the message, the handler still has the original size of the message (contained in msg.arg1), but the buffer data has been updated (/overwritten).

I can see three ways to address this issue: 1. Use the android recommended mechanism (create a bundle of data, and use .setdata() to attach this to the message). I've not tried this, but I expect the bundle creation will result in the data being copied out of the buffer byte array. 2. use new memory for message data. This can be by run-time allocating (but that conflicts with the 'light weight' intention of messaging), or by using multiple statically allocated buffers, and cycling between them. Either approach has issues. 3. perform collection in the low-level thread 'run()', and only send a message for completed Bluetooth messages.

I opted for the third method. In my case, bluetooth messages contain terminated strings, so I use a string builder to collect bytes returned by mmInStream.read(), and only send a message to the handler when the end of line is detected.

A different way is to check if there is already any message with the same name in the Handler's queue with .hasMessages(). Example:

public void run() {
     DebugLog.i("BluetoothService", "BEGIN mConnectedThread");
     byte[] buffer = new byte[1024];
     int bytes;
         // Keep listening to the InputStream while connected
     while (true) {
          //Check if there is no pending similar message on the queue
          if (!mHandler.hasMessages(Constants.MESSAGE_READ)) {
              try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);

                DebugLog.d("BluetoothService", new String(buffer, 0, bytes));
                 // Send the obtained bytes to the UI Activity
                 mHandler.obtainMessage(MESSAGE_READ, bytes, -1, buffer)
                          .sendToTarget();
              } catch (IOException e) {
                    DebugLog.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
              }
          }
     }
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!