The answer was that it was an Xcode bug. Fixed with Xcode 5.1.1.
From the Xcode release log:
Resolved Issues: Crash in compiled code when targeting iOS 5.1.1 (16485980)
题
My app runs fine if compiler optimization = None [-O0].
Crashes consistently at one point if optimization = Fast [-O, -O1], crashes consistently at a different point if set to -O2, -O3 or -Ofast, and at a different point if set to -Os.
The crashes all happen in the viewDidLoad
start-up sequence and are always EXC_BAD_ACCESS
.
The crashes only happen when running on the iPad1/iOS5.1.1. No crashes on iPad4/iOS7.1, regardless of optimization setting.
It used to be fine, but it broke sometime recently - having rolled back to previously working versions which are now also crashing, it looks like it might be due to Xcode/SDK version.
Am using Xcode 5.1 on Mac OS X 10.8.5. Base SDK = Latest iOS (iOS 7.1); iOS Deployment Target = iOS 5.0.
My question is: based on the above info, what's the best approach to debug from here on? Am a bit stuck.
Edit: here is the crash log using -O3:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x6454f8e3
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libstdc++.6.dylib 0x36b78cd8 std::basic_ios<char, std::char_traits<char> >::tie() const + 0
1 libstdc++.6.dylib 0x36b91544 std::ostream::sentry::sentry(std::ostream&) + 20
2 libstdc++.6.dylib 0x36b93b86 std::basic_ostream<char, std::char_traits<char> >& std::__ostream_insert<char, std::char_traits<char> >(std::basic_ostream<char, std::char_traits<char> >&, char const*, int) + 22
3 KP v2 Dev 0x0001817e performerApp::setBankSettingsFromXmlStruct(int) (performerApp.mm:2607)
4 KP v2 Dev 0x0000cfc8 performerApp::setBankSettingsFromExternalFile(int) (performerApp.mm:2892)
5 KP v2 Dev 0x00009862 performerApp::setup() (performerApp.mm:141)
6 KP v2 Dev 0x00347750 Poco::PriorityDelegate<ofBaseApp, ofEventArgs, false>::notify(void const*, ofEventArgs&) (PriorityDelegate.h:168)
7 KP v2 Dev 0x002e5dfa Poco::AbstractEvent<ofEventArgs, Poco::PriorityStrategy<ofEventArgs, Poco::AbstractPriorityDelegate<ofEventArgs> >, Poco::AbstractPriorityDelegate<ofEventArgs>, Poco::FastMutex>::notify(void const*, ofEventArgs&) (PriorityStrategy.h:81)
8 KP v2 Dev 0x002e31a0 ofNotifySetup() (ofEventUtils.h:172)
9 KP v2 Dev 0x00341246 -[ofxiOSEAGLView setup] (ofxiOSEAGLView.mm:0)
10 KP v2 Dev 0x003498dc -[ofxiOSViewController viewDidLoad] (ofxiOSViewController.mm:54)
11 UIKit 0x32c78c84 -[UIViewController view] + 160
12 UIKit 0x32c7745a -[UIWindow addRootViewControllerViewIfPossible] + 38
13 UIKit 0x32dfb774 -[UIWindow setRootViewController:] + 332
14 KP v2 Dev 0x00348706 -[ofxiOSAppDelegate applicationDidFinishLaunching:] (ofxiOSAppDelegate.mm:170)
15 UIKit 0x32c77cfa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1262
16 UIKit 0x32c717d6 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 402
17 UIKit 0x32c3fabc -[UIApplication handleEvent:withNewEvent:] + 1004
18 UIKit 0x32c3f560 -[UIApplication sendEvent:] + 48
19 UIKit 0x32c3ef34 _UIApplicationHandleEvent + 5820
20 GraphicsServices 0x33231224 PurpleEventCallback + 876
21 CoreFoundation 0x3551351c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 32
22 CoreFoundation 0x355134be __CFRunLoopDoSource1 + 134
23 CoreFoundation 0x3551230c __CFRunLoopRun + 1364
24 CoreFoundation 0x3549549e CFRunLoopRunSpecific + 294
25 CoreFoundation 0x35495366 CFRunLoopRunInMode + 98
26 UIKit 0x32c70864 -[UIApplication _run] + 544
27 UIKit 0x32c6dcce UIApplicationMain + 1074
28 KP v2 Dev 0x00340600 ofAppiOSWindow::startAppWithDelegate(std::string) (ofAppiOSWindow.mm:96)
29 KP v2 Dev 0x0034047e ofAppiOSWindow::runAppViaInfiniteLoop(ofBaseApp*) (ofAppiOSWindow.mm:84)
30 KP v2 Dev 0x002dce9e ofRunApp(ofBaseApp*) (ofAppRunner.cpp:137)
31 KP v2 Dev 0x00008f10 main (main.mm:32)
32 KP v2 Dev 0x00008e00 start + 32
Here is the crash log using -Os:
Exception Type: EXC_BAD_ACCESS (SIGSEGV)
Exception Codes: KERN_INVALID_ADDRESS at 0x54442d1c
Crashed Thread: 0
Thread 0 name: Dispatch queue: com.apple.main-thread
Thread 0 Crashed:
0 libstdc++.6.dylib 0x36b6ab90 std::ios_base::_M_call_callbacks(std::ios_base::event) + 88
1 libstdc++.6.dylib 0x36b6ae48 std::ios_base::~ios_base() + 20
2 KP v2 Dev 0x00031d38 std::string ofToString<int>(int const&) (basic_ios.h:268)
3 KP v2 Dev 0x000593f4 BankData (BankData.mm:27)
4 KP v2 Dev 0x00058bf2 ConfigData::init(int) (ConfigData.mm:28)
5 KP v2 Dev 0x0000c082 performerApp::setup() (performerApp.mm:52)
6 KP v2 Dev 0x00343700 Poco::PriorityDelegate<ofBaseApp, ofEventArgs, false>::notify(void const*, ofEventArgs&) (PriorityDelegate.h:168)
7 KP v2 Dev 0x002e1daa Poco::AbstractEvent<ofEventArgs, Poco::PriorityStrategy<ofEventArgs, Poco::AbstractPriorityDelegate<ofEventArgs> >, Poco::AbstractPriorityDelegate<ofEventArgs>, Poco::FastMutex>::notify(void const*, ofEventArgs&) (PriorityStrategy.h:81)
8 KP v2 Dev 0x002df150 ofNotifySetup() (ofEventUtils.h:172)
9 KP v2 Dev 0x0033d1f6 -[ofxiOSEAGLView setup] (ofxiOSEAGLView.mm:0)
10 KP v2 Dev 0x0034588c -[ofxiOSViewController viewDidLoad] (ofxiOSViewController.mm:54)
11 UIKit 0x32c78c84 -[UIViewController view] + 160
12 UIKit 0x32c7745a -[UIWindow addRootViewControllerViewIfPossible] + 38
13 UIKit 0x32dfb774 -[UIWindow setRootViewController:] + 332
14 KP v2 Dev 0x003446b6 -[ofxiOSAppDelegate applicationDidFinishLaunching:] (ofxiOSAppDelegate.mm:170)
15 UIKit 0x32c77cfa -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1262
16 UIKit 0x32c717d6 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 402
17 UIKit 0x32c3fabc -[UIApplication handleEvent:withNewEvent:] + 1004
18 UIKit 0x32c3f560 -[UIApplication sendEvent:] + 48
19 UIKit 0x32c3ef34 _UIApplicationHandleEvent + 5820
20 GraphicsServices 0x33231224 PurpleEventCallback + 876
21 CoreFoundation 0x3551351c __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 32
22 CoreFoundation 0x355134be __CFRunLoopDoSource1 + 134
23 CoreFoundation 0x3551230c __CFRunLoopRun + 1364
24 CoreFoundation 0x3549549e CFRunLoopRunSpecific + 294
25 CoreFoundation 0x35495366 CFRunLoopRunInMode + 98
26 UIKit 0x32c70864 -[UIApplication _run] + 544
27 UIKit 0x32c6dcce UIApplicationMain + 1074
28 KP v2 Dev 0x0033c5b0 ofAppiOSWindow::startAppWithDelegate(std::string) (ofAppiOSWindow.mm:96)
29 KP v2 Dev 0x0033c42e ofAppiOSWindow::runAppViaInfiniteLoop(ofBaseApp*) (ofAppiOSWindow.mm:84)
30 KP v2 Dev 0x002d8e4e ofRunApp(ofBaseApp*) (ofAppRunner.cpp:137)
31 KP v2 Dev 0x0000bbe0 main (main.mm:32)
32 KP v2 Dev 0x0000bad0 start + 32
解决方案 2
The answer was that it was an Xcode bug. Fixed with Xcode 5.1.1.
From the Xcode release log:
Resolved Issues: Crash in compiled code when targeting iOS 5.1.1 (16485980)
其他提示
Often when something crashes at higher optimizations but not at -O0 it means you are either using a variable that is uninitialized, or using an object after it has been freed. At -O0 lots of data is left sitting around after it is done with, whereas at higher optimizations it is more aggressively reused. The Xcode static analyzer is very handy for finding uninitialized variable use. And running with Guard Malloc (settable in the Diagnostics pane of the Xcode Run scheme) will catch use after free at the point where it occurs, rather than sometime much further downstream.