Recently discovered a bug of iOS which will cause random crash.
According to researches, the crash will appear in the possibility of range from > 0.1% to 2%.
As most experienced programmer know, this is extremely annoying type of bug.
The annoying-ness is due to the randomness, which is very hard to trace and catch.
This type of bug may spending weeks to solve one.
[ Symptom ]
If console is available, the console output should be similar to
<Error>: *** Terminating app due to uncaught exception ‘NSFileHandleOperationException’, reason: ‘*** -[NSConcreteFileHandle writeData:]: Bad file descriptor’
*** First throw call stack:
(0x3302f88f 0x34fd4259 0x3302f789 0x3302f7ab 0x376dcd15 0x376ba2a3 0xb8cfd 0xd19ad 0x3556ecab 0x355687dd 0x35536ac3 0x35536567 0x35535f3b 0x330d322b 0x33003523 0x330034c5 0x33002313 0x32f854a5 0x32f8536d 0x3556786b 0x35564cd5 0x91fff 0x91fb4)
Jun 6 19:46:30 unknown ReportCrash[270] <Notice>: Formulating crash report for process APP_NAME[268]
Jun 6 19:46:30 unknown com.apple.launchd[1] <Warning>: (UIKitApplication:com.APP_NAME.APP_NAME[0xdd2e]) Job appears to have crashed: Abort trap: 6
Jun 6 19:46:30 unknown SpringBoard[23] <Warning>: Application ‘APP_NAME’ exited abnormally with signal 6: Abort trap: 6
Jun 6 19:46:30 unknown ReportCrash[270] <Notice>: Saved crashreport to /var/mobile/Library/Logs/CrashReporter/APP_NAME_2012-06-06-194630_IPAD_NAME.plist using uid: 0 gid: 0, synthetic_euid: 501 egid: 0
The crash log should be like:
Incident Identifier: 16C417D3-236D-473E-B10B-A9072AE69273
CrashReporter Key: e69210ad08c63842187ed72017cd40c0675b8b69
Hardware Model: iPad3,3
Process: APP_NAME [11251]
…
…
0 libsystem_kernel.dylib 0x352f8004 mach_msg_trap + 20
1 libsystem_kernel.dylib 0x352f81fa mach_msg + 50
2 CoreFoundation 0x32e053ec __CFRunLoopServiceMachPort + 120
3 CoreFoundation 0x32e04124 __CFRunLoopRun + 876
4 CoreFoundation 0x32d8749e CFRunLoopRunSpecific + 294
5 CoreFoundation 0x32d87366 CFRunLoopRunInMode + 98
6 CFNetwork 0x36b59e14 CFURLConnectionSendSynchronousRequest + 340
7 Foundation 0x374854a6 +[NSURLConnection sendSynchronousRequest:returningResponse:error:] + 242
8 APP_NAME 0x0006a6fe 0x26000 + 280318
9 APP_NAME 0x0006a5fa 0x26000 + 280058
10 APP_NAME 0x00068034 0x26000 + 270388
11 APP_NAME 0x00067bc6 0x26000 + 269254
12 UIKit 0x35370ca4 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1176
13 UIKit 0x3536a7d6 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 402
14 UIKit 0x35338abc -[UIApplication handleEvent:withNewEvent:] + 1004
15 UIKit 0x35338560 -[UIApplication sendEvent:] + 48
……
……
[Solution]
The cause of the bug, is traceable back to the [NSURLConnection sendSynchronousRequest:returningResponse:error:]
responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error];
The solution is:
1. Check if the URL opening is a redirecting URL. If so, change it to be non-redirecting.
2. Change the synchronous request to asynchronous request, with delegate.
3. Add this implementation in the handling:
– (NSURLRequest *)connection:(NSURLConnection *)connection
willSendRequest:(NSURLRequest *)request
redirectResponse:(NSURLResponse *) redirectResponse{
return request;
}
Done.