【发布时间】:2014-02-23 09:20:23
【问题描述】:
我正在尝试编写一个使用 USB 中断传输与 USB 设备通信的服务。基本上我在线程中阻塞 UsbConnection.requestWait() 以等待中断传输,然后使用意图将这些传递给活动。
当 USB 设备连续向我发送大量中断数据包(大约 50 个)时,我似乎遇到了问题。它有时会起作用,但通常应用程序会崩溃并显示这种风格的消息:
02-23 01:55:53.387: A/libc(8460): @@@ ABORTING: heap corruption detected by tmalloc_small
02-23 01:55:53.387: A/libc(8460): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 8460 (pf.mustangtamer)
malloc 调用并不总是失败,我见过几种 malloc(dlmalloc、malloc_small)和 dlfree。在每种情况下,我都会收到致命信号 11 和对 0xdeadbaad 的引用,因此我以某种方式破坏了堆。
从堆转储中看不出是什么导致了损坏。
这是我认为有问题的代码(仅在背靠背接收到许多数据包时才会出现问题):
private class ReceiverThread extends Thread {
public ReceiverThread(String string) {
super(string);
}
public void run() {
ByteBuffer buffer = ByteBuffer.allocate(BUFFER_SIZE);
buffer.clear();
UsbRequest inRequest = new UsbRequest();
inRequest.initialize(mUsbConnection, mUsbEndpointIn);
while(mUsbDevice != null ) {
if (inRequest.queue(buffer, BUFFER_SIZE) == true) {
// (mUsbConnection.requestWait() is blocking
if (mUsbConnection.requestWait() == inRequest){
buffer.flip();
byte[] bytes = new byte[buffer.remaining()];
buffer.get(bytes);
//TODO: use explicit intent, not broadcast
Intent intent = new Intent(RECEIVED_INTENT);
intent.putExtra(DATA_EXTRA, bytes);
sendBroadcast(intent);
} else{
Log.d(TAG, "mConnection.requestWait() returned for a different request (likely a send operation)");
}
} else {
Log.e(TAG, "failed to queue USB request");
}
buffer.clear();
}
Log.d(TAG, "RX thread terminating.");
}
}
现在活动没有消耗意图,我正在尝试让 USB 通信停止崩溃,然后再实施该端。
我没有看到上面的代码如何破坏堆,可能是通过一些非线程安全的行为。一次只有一个请求排队,所以我认为“缓冲区”是安全的。 我的目标是运行 JB 4.3.1 的平板电脑,如果这有影响的话。
【问题讨论】:
标签: android usb heap-corruption