vrwim
vrwim

Reputation: 14380

SwiftUI Preview crashes with stackoverflow

In an existing app I wanted to try out SwiftUI for a few small views. I opened my project in macOS Catalina and Xcode 11 and added a SwiftUI class.

To my surprise, I got the following error:

Cannot preview in this file -- YourApp.app may have crashed

at the top of the preview window.

When I look at the crashlog, I see it's because of a stackoverflow error:

Crashed Thread: 0 Dispatch queue: com.apple.main-thread

Exception Type:        EXC_BAD_ACCESS (SIGSEGV)
Exception Codes:       KERN_PROTECTION_FAILURE at 0x00007ffee98cfff8
Exception Note:        EXC_CORPSE_NOTIFY

Termination Signal:    Segmentation fault: 11
Termination Reason:    Namespace SIGNAL, Code 0xb
Terminating Process:   exc handler [37403]

External Modification Warnings:
Thread creation by external task.

VM Regions Near 0x7ffee98cfff8:
    MALLOC_SMALL           00007fa424000000-00007fa424800000 [ 8192K] rw-/rwx SM=PRV  
--> STACK GUARD            00007ffee60d0000-00007ffee98d0000 [ 56.0M] ---/rwx SM=NUL  stack guard for thread 0
    Stack                  00007ffee98d0000-00007ffeea0d0000 [ 8192K] rw-/rwx SM=ALI  thread 0

Application Specific Information:
CoreSimulator 643.8.4 - Device: iPhone Xʀ (1C2CE9A2-334D-4A62-8AE9-C6CCC6D346E6) - Runtime: iOS 13.0 (17A5492t) - DeviceType: iPhone Xʀ

Thread 0 Crashed:: Dispatch queue: com.apple.main-thread
0   MessageView.2.preview-thunk.dylib   0x000000012f726918 -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 34
1   MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
2   MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
3   MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
...
508 MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
509 MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
510 MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37
511 MessageView.2.preview-thunk.dylib   0x000000012f72691b -[UIViewController(PageViewLogging) ms_viewWillAppear:] + 37

Upvotes: 0

Views: 2468

Answers (2)

X1opya
X1opya

Reputation: 61

Yeah, its happen while your AppDelegate's method didFinishLaunchingWithOptions have some external libs initializations (For example Objective-C Promises crashes in SwiftUI previews or SwiftUI previews not working when using Firebase/Analytics pod

Add this check at start of didFinishLaunchingWithOptions and preview mod will ignoring external modules

if ProcessInfo.processInfo.environment["XCODE_RUNNING_FOR_PREVIEWS"] == "1" {
    return true
}

Upvotes: 1

vrwim
vrwim

Reputation: 14380

This is because you use external frameworks in your didFinishLaunchingWithOptions of your AppDelegate. Clear that method while you work on your SwiftUI code and it will work as it's supposed to.

Not entirely sure why this is, but it may be because SwiftUI runs an instance of your app to render it, and uses AppDelegate to do that.

Upvotes: 1

Related Questions