App crashes on start on real device only

1

I wanted to publish my game app today(using Swift - xCode GTM and Sprite kit framework). My app works fine on the simulator but when I tried it on a real device - iPhone 4 iOS 7, it crashes on start:

class func unarchiveFromFile(file : NSString) -> SKNode? {

        let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks")//CRASH!

        var sceneData = NSData.dataWithContentsOfFile(path!, options: .DataReadingMappedIfSafe, error: nil)
        var archiver = NSKeyedUnarchiver(forReadingWithData: sceneData)

        archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
        let scene = archiver.decodeObjectForKey(NSKeyedArchiveRootObjectKey) as GameScene
        archiver.finishDecoding()
        return scene
    }
}

here is my Crash log:

Exception Type:  EXC_BREAKPOINT (SIGTRAP)
Exception Codes: 0x0000000000000001, 0x000000000000defe
Triggered by Thread:  0

Thread 0 Crashed:
0   libswiftCore.dylib              0x003347d0 0x1b6000 + 1566672
1   libswiftCore.dylib              0x001f3a64 0x1b6000 + 252516
2   MyApp                       0x000d2904 0xc4000 + 59652
3   MyApp                       0x000d36d8 0xc4000 + 63192
4   MyApp                       0x000d4214 0xc4000 + 66068
5   MyApp                       0x000d4dc4 0xc4000 + 69060
6   UIKit                           0x3079e5fe -[UIViewController loadViewIfRequired] + 514
7   UIKit                           0x3079e3bc -[UIViewController view] + 20
8   UIKit                           0x307a5088 -[UIWindow addRootViewControllerViewIfPossible] + 60
9   UIKit                           0x307a275e -[UIWindow _setHidden:forced:] + 302
10  UIKit                           0x3080da50 -[UIWindow makeKeyAndVisible] + 56
11  UIKit                           0x3080a80c -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1652
12  UIKit                           0x30804d0e -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 710
13  UIKit                           0x3079f6a2 -[UIApplication handleEvent:withNewEvent:] + 3126
14  UIKit                           0x3079e9a4 -[UIApplication sendEvent:] + 68
15  UIKit                           0x308044f8 _UIApplicationHandleEvent + 660
16  GraphicsServices                0x32c3670a _PurpleEventCallback + 606
17  GraphicsServices                0x32c362f2 PurpleEventCallback + 30
18  CoreFoundation                  0x2dff39e4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 32
19  CoreFoundation                  0x2dff397e __CFRunLoopDoSource1 + 342
20  CoreFoundation                  0x2dff2152 __CFRunLoopRun + 1394
21  CoreFoundation                  0x2df5cce2 CFRunLoopRunSpecific + 518
22  CoreFoundation                  0x2df5cac6 CFRunLoopRunInMode + 102
23  UIKit                           0x30803794 -[UIApplication _run] + 756
24  UIKit                           0x307fea3c UIApplicationMain + 1132
25  MyApp                       0x000da594 0xc4000 + 91540
26  MyApp                       0x000da5d0 0xc4000 + 91600
27  libdyld.dylib                   0x38858ab4 start + 0

Thread 1:
0   libsystem_kernel.dylib          0x388fc83c kevent64 + 24
1   libdispatch.dylib               0x38847e08 _dispatch_mgr_invoke + 228
2   libdispatch.dylib               0x388373ee _dispatch_mgr_thread$VARIANT$up + 34

Thread 2:
0   libsystem_kernel.dylib          0x3890fc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x38975dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x38975c80 start_wqthread + 4

Thread 3:
0   libsystem_kernel.dylib          0x3890fc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x38975dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x38975c80 start_wqthread + 4

Thread 4 name:  WebThread
Thread 4:
0   libsystem_kernel.dylib          0x3890efa8 __psynch_mutexwait + 24
1   libsystem_pthread.dylib         0x38975f0a _pthread_mutex_lock + 302
2   WebCore                         0x35e2f138 _WebTryThreadLock(bool) + 104
3   WebCore                         0x35e2f0ba WebRunLoopLock(__CFRunLoopObserver*, unsigned long, void*) + 42
4   CoreFoundation                  0x2dff41d2 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 18
5   CoreFoundation                  0x2dff1b74 __CFRunLoopDoObservers + 280
6   CoreFoundation                  0x2dff1f84 __CFRunLoopRun + 932
7   CoreFoundation                  0x2df5cce2 CFRunLoopRunSpecific + 518
8   CoreFoundation                  0x2df5cac6 CFRunLoopRunInMode + 102
9   WebCore                         0x35e2dbae RunWebThread(void*) + 414
10  libsystem_pthread.dylib         0x38977c1a _pthread_body + 138
11  libsystem_pthread.dylib         0x38977b8a _pthread_start + 98
12  libsystem_pthread.dylib         0x38975c8c thread_start + 4

Thread 5 name:  com.apple.NSURLConnectionLoader
Thread 5:
0   libsystem_kernel.dylib          0x388fca8c mach_msg_trap + 20
1   libsystem_kernel.dylib          0x388fc888 mach_msg + 44
2   CoreFoundation                  0x2dff37c6 __CFRunLoopServiceMachPort + 150
3   CoreFoundation                  0x2dff1eec __CFRunLoopRun + 780
4   CoreFoundation                  0x2df5cce2 CFRunLoopRunSpecific + 518
5   CoreFoundation                  0x2df5cac6 CFRunLoopRunInMode + 102
6   Foundation                      0x2e996492 +[NSURLConnection(Loader) _resourceLoadLoop:] + 314
7   Foundation                      0x2ea0be22 __NSThread__main__ + 1058
8   libsystem_pthread.dylib         0x38977c1a _pthread_body + 138
9   libsystem_pthread.dylib         0x38977b8a _pthread_start + 98
10  libsystem_pthread.dylib         0x38975c8c thread_start + 4

Thread 6:
0   libsystem_kernel.dylib          0x388fca8c mach_msg_trap + 20
1   libsystem_kernel.dylib          0x388fc888 mach_msg + 44
2   CoreFoundation                  0x2dff37c6 __CFRunLoopServiceMachPort + 150
3   CoreFoundation                  0x2dff1eec __CFRunLoopRun + 780
4   CoreFoundation                  0x2df5cce2 CFRunLoopRunSpecific + 518
5   CoreFoundation                  0x2df5cac6 CFRunLoopRunInMode + 102
6   libAVFAudio.dylib               0x2cfe1584 GenericRunLoopThread::Entry(void*) + 124
7   libAVFAudio.dylib               0x2cfd5a94 CAPThread::Entry(CAPThread*) + 176
8   libsystem_pthread.dylib         0x38977c1a _pthread_body + 138
9   libsystem_pthread.dylib         0x38977b8a _pthread_start + 98
10  libsystem_pthread.dylib         0x38975c8c thread_start + 4

Thread 7:
0   libsystem_kernel.dylib          0x3890fc7c __workq_kernreturn + 8
1   libsystem_pthread.dylib         0x38975dc6 _pthread_wqthread + 306
2   libsystem_pthread.dylib         0x38975c80 start_wqthread + 4

Thread 8 name:  JavaScriptCore::BlockFree
Thread 8:
0   libsystem_kernel.dylib          0x3890ef38 __psynch_cvwait + 24
1   libsystem_pthread.dylib         0x38977224 _pthread_cond_wait + 536
2   libsystem_pthread.dylib         0x38978040 pthread_cond_timedwait + 40
3   JavaScriptCore                  0x2ef80eb8 WTF::ThreadCondition::timedWait(WTF::Mutex&, double) + 104
4   JavaScriptCore                  0x2ef80ce4 JSC::BlockAllocator::blockFreeingThreadMain() + 88
5   JavaScriptCore                  0x2ef7e3a8 WTF::wtfThreadEntryPoint(void*) + 12
6   libsystem_pthread.dylib         0x38977c1a _pthread_body + 138
7   libsystem_pthread.dylib         0x38977b8a _pthread_start + 98
8   libsystem_pthread.dylib         0x38975c8c thread_start + 4

Thread 0 crashed with ARM Thread State (32-bit):
    r0: 0x00000000    r1: 0x00000000      r2: 0x00362ac4      r3: 0x000000b6
    r4: 0x00362ac4    r5: 0x00000000      r6: 0x145c97b0      r7: 0x27d3d364
    r8: 0x1456eb50    r9: 0x00000fff     r10: 0x145c97b0     r11: 0x00362ac0
    ip: 0x3a5431d0    sp: 0x27d3d354      lr: 0x00334183      pc: 0x003347d0
  cpsr: 0x60000030
ios
xcode
swift
sprite-kit
asked on Stack Overflow Sep 11, 2014 by user3897399

1 Answer

1

The pathForResource() function returns a String?, indicating that (because it's an optional) there's a chance no value will be returned. I'm guessing the method is returning nil, but because you're assigning it using a let statement, the app is crashing.

I'm a little surprised it's crashing directly on that line though. You should be able to assign nil using the let keyword. In any case, if the value is nil, you'd definitely encounter a crash on your next line when you force unwrap the optional:

var sceneData = NSData.dataWithContentsOfFile(path!, options: .DataReadingMappedIfSafe, error: nil)
                                                  ^ here

Try this:

if let path = NSBundle.mainBundle().pathForResource(file, ofType: "sks") {
    /* ... (your code using path) ... */
} else {
    NSLog("Unable to locate path for resource: \(file)")
}

I have a feeling you'll see that NSLog() output at launch.

answered on Stack Overflow Sep 11, 2014 by Craig Otis • edited Sep 11, 2014 by Craig Otis

User contributions licensed under CC BY-SA 3.0