can AsyncTask be interrupted when running native code? weird things happening
I am having unexpected behavior within a native C function, which is called by an AsyncTask thread. The code is pretty straight forward. I do error checking on memory which I am going to copy a string in to. A set of messages is sent to the debug. However, despite all of the checks I run on the variable, I still get a segfault in an strncpy(). Also, the debug messages are missed as if the task was interru开发者_JAVA百科pted and not resumed at that location.
// 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;
}
Despite all of these checks, I get a segfault on strncpy that I am trying to debug:
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);
First, the malloc always succeeds and never returns NULL. Here comes the odd part, this is the debug output:
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...
The first thing that should be noted is the number within the bracket's, e.g., "[#]: should..." That number is a packet number, and for each packet number one of the incremental check messages should be printed, then one of the resulting messages: "trying to copy string" or "not copying, bailing."
However, as you can see from the debug output, the first message is "INFO/WiresharkDriver(4171): [289]: dissection->fields->field_values[0] is null when trying to copy string", but then there is no followup message associated with that packet. How can this be? Can the AsyncTask be interrupted, and then continue executing in a different location?
精彩评论