运行本机代码时可以中断AsyncTask吗?发生了奇怪的事情

时间:2011-09-01 04:27:40

标签: android android-ndk android-asynctask interrupt

我在本机C函数中有意外行为,由AsyncTask线程调用。代码很简单。我对内存进行错误检查,我要将字符串复制到。一组消息被发送到调试。但是,尽管我在变量上运行了所有检查,但我仍然在strncpy()中得到段错误。此外,调试消息将被遗漏,就像任务被中断而在该位置没有恢复一样。

// First, allocate memory to copy in a string, ensure pointer not NULL
char *str_res = malloc(1024);
if(str_res==NULL)
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "error allocating memory for return string, doh");
memset(str_res, '\0', sizeof(str_res));

// Do incremental checks that the source string is valid
if(dissection == NULL)
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: dissection is null when trying to copy string", _pkt_count);
else if(dissection->fields == NULL)
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: dissection->fields is null when trying to copy string", _pkt_count);
else if(dissection->fields->field_values == NULL)
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: dissection->fields->field_values is null when trying to copy string", _pkt_count);
else if(dissection->fields->field_values[0] == NULL)
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: dissection->fields->field_values[0] is null when trying to copy string", _pkt_count);
else
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: should be trying to copy string", _pkt_count);

// Do one last check, then strncpy if the source string is not null
if(dissection->fields->field_values[0]!=NULL) {
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: trying to copy string...", _pkt_count);
  strncpy(str_res, dissection->fields->field_values[0]->str, 1023);
} else {
  __android_log_print(ANDROID_LOG_INFO, LOG_TAG, "[%d]: not copying, bailing", _pkt_count);
  myoutput_fields_free(dissection->fields);
  free(str_res);
  return NULL;
}

尽管进行了所有这些检查,但我在strncpy上遇到了一个我试图调试的段错误:

Program received signal SIGSEGV, Segmentation fault.
[Switching to Thread 4282]
0x80d03b74 in wiresharkGet (wfd_ptr=8425344, field=0x80a128 "wlan_mgt.fixed.beacon") at /Users/gnychis/Documents/workspace/CoexiSyst/jni/libwireshark/wireshark_helper.c:686
686         strncpy(str_res, dissection->fields->field_values[0]->str, 1023);

首先,malloc总是成功,永远不会返回NULL。这是奇怪的部分,这是调试输出:

INFO/WiresharkDriver(4171): [289]: dissection->fields->field_values[0] is null when trying to copy string
DEBUG/WiFiScanReceiver(4171): Received incoming scan complete message
INFO/WiresharkDriver(4171): [290]: should be trying to copy string
INFO/WiresharkDriver(4171): [290]: trying to copy string...
INFO/WiresharkDriver(4171): [291]: should be trying to copy string
INFO/WiresharkDriver(4171): [291]: trying to copy string...
INFO/WiresharkDriver(4171): [292]: should be trying to copy string
INFO/WiresharkDriver(4171): [292]: trying to copy string...
INFO/WiresharkDriver(4171): [293]: should be trying to copy string
INFO/WiresharkDriver(4171): [293]: trying to copy string...
INFO/WiresharkDriver(4171): [294]: should be trying to copy string
INFO/WiresharkDriver(4171): [294]: trying to copy string...
INFO/WiresharkDriver(4171): [294]: trying to copy string...

应该注意的第一件事是括号内的数字,例如“[#]:应该......”这个数字是一个包号,对于每个包号,应该打印一个增量检查消息,然后得到一条消息:“试图复制字符串”或“不复制,哄骗。”

但是,正如您从调试输出中看到的那样,第一条消息是“INFO / WiresharkDriver(4171):[289]:解析 - > fields-> field_values [0]在尝试复制字符串时为空” ,但是没有与该数据包关联的后续消息。怎么会这样?是否可以中断AsyncTask,然后继续在其他位置执行?

0 个答案:

没有答案