Repeated Crash Reports when I add code to a project

I am doing the Swift Landmarks app tutorial on Xcode 13.2.1 on my 2019 Macbook Pro. I am up to the Working with UI Controls section. Every time I add the following lines of code to ProfileSummary.swift:

@EnvironmentObject var modelData: ModelData
.
.
.
Divider()
         
        VStack(alignment: .leading)
        {
                  Text("Recent Hikes")
                    .font(.headline)
                  HikeView(hike: modelData.hikes[0])
        }

and I then try to view the preview I repeatedly get a crash report and everything stops working. When I remove those lines of code everything goes back to working fine.

The crash report is in this attachment:

Translated Report (Full Report Below)
-------------------------------------

Incident Identifier: DD81BDB8-BE7F-4DA1-A56E-45025DAF7A4D
CrashReporter Key:   17A2CDBF-DE4D-F9C2-89AE-80565996F518
Hardware Model:      MacBookPro16,1
Process:             Landmarks [5629]
Path:                /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/4DBA156B-3EA5-4245-80BD-56F3BCBDCA93/data/Containers/Bundle/Application/8672C598-D194-4AD1-B640-4AF093632FA2/Landmarks.app/Landmarks
Identifier:          ICO.Landmarks
Version:             1.0 (1)
Code Type:           X86-64 (Native)
Role:                Foreground
Parent Process:      launchd_sim [5245]
Coalition:           com.apple.CoreSimulator.SimDevice.4DBA156B-3EA5-4245-80BD-56F3BCBDCA93 [2928]
Responsible Process: SimulatorTrampoline [1412]

Date/Time:           2022-02-15 15:12:56.0663 -0500
Launch Time:         2022-02-15 15:12:55.8962 -0500
OS Version:          macOS 12.2.1 (21D62)
Release Type:        User
Report Version:      104

Exception Type:  EXC_BAD_INSTRUCTION (SIGILL)
Exception Codes: 0x0000000000000001, 0x0000000000000000
Exception Note:  EXC_CORPSE_NOTIFY
Termination Reason: SIGNAL 4 Illegal instruction: 4
Terminating Process: exc handler [5629]

Triggered by Thread:  0

Application Specific Information:
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/Landmarks-ggpnlakhqjqzlsaruntpdkhagabp/Build/Intermediates.noindex/Previews/Landmarks/Products/Debug-iphonesimulator DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot//System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection DYLD_FRAMEWORK_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/Landmarks-ggpnlakhqjqzlsaruntpdkhagabp/Build/Intermediates.noindex/Previews/Landmarks/Products/Debug-iphonesimulator
CoreSimulator 783.5 - Device: iPhone 13 Pro (4DBA156B-3EA5-4245-80BD-56F3BCBDCA93) - Runtime: iOS 15.2 (19C51) - DeviceType: iPhone 13 Pro
SwiftUI/EnvironmentObject.swift:70: Fatal error: No ObservableObject of type ModelData found. A View.environmentObject(_:) for ModelData may be missing as an ancestor of this view.
dyld4 config: DYLD_ROOT_PATH=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot DYLD_LIBRARY_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/Landmarks-ggpnlakhqjqzlsaruntpdkhagabp/Build/Intermediates.noindex/Previews/Landmarks/Products/Debug-iphonesimulator DYLD_INSERT_LIBRARIES=/Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot//System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection DYLD_FRAMEWORK_PATH=/Users/apple/Library/Developer/Xcode/DerivedData/Landmarks-ggpnlakhqjqzlsaruntpdkhagabp/Build/Intermediates.noindex/Previews/Landmarks/Products/Debug-iphonesimulator


Thread 0 Crashed::  Dispatch queue: BSXPCCnx:com.apple.dt.xcode-previews.systemservices (BSCnx:client:com.apple.dt.uv.agent-preview-nonui-service)
0   libswiftCore.dylib            	    0x7fff3073ff1b _assertionFailure(_:_:file:line:flags:) + 427
1   SwiftUI                       	    0x7fff5d2aef2d EnvironmentObject.error() + 243
2   SwiftUI                       	    0x7fff5d2aee3a EnvironmentObject.wrappedValue.getter + 19
3   Landmarks                     	       0x10cf74bee ProfileSummary.modelData.getter + 142
4   ProfileSummary.1.preview-thunk.dylib	       0x10f21af06 closure #2 in closure #1 in closure #1 in ProfileSummary.__preview__body.getter + 950 (ProfileSummary.swift:54)
5   ProfileSummary.1.preview-thunk.dylib	       0x10f21c77d partial apply for closure #2 in closure #1 in closure #1 in ProfileSummary.__preview__body.getter + 13
6   ProfileSummary.1.preview-thunk.dylib	       0x10f21b367 thunk for @callee_guaranteed () -> (@owned TupleView<(Text, HikeView)>) + 23
7   ProfileSummary.1.preview-thunk.dylib	       0x10f21c791 partial apply for thunk for @callee_guaranteed () -> (@owned TupleView<(Text, HikeView)>) + 17
8   SwiftUI                       	    0x7fff5ce16d23 VStack.init(alignment:spacing:content:) + 159
9   ProfileSummary.1.preview-thunk.dylib	       0x10f2190fe closure #1 in closure #1 in ProfileSummary.__preview__body.getter + 4510 (ProfileSummary.swift:49)
10  ProfileSummary.1.preview-thunk.dylib	       0x10f21b7dd partial apply for closure #1 in closure #1 in ProfileSummary.__preview__body.getter + 13
11  SwiftUI                       	    0x7fff5ce16d23 VStack.init(alignment:spacing:content:) + 159
12  ProfileSummary.1.preview-thunk.dylib	       0x10f217ed6 closure #1 in ProfileSummary.__preview__body.getter + 422 (ProfileSummary.swift:17)
13  ProfileSummary.1.preview-thunk.dylib	       0x10f217f5d partial apply for closure #1 in ProfileSummary.__preview__body.getter + 13
14  SwiftUI                       	    0x7fff5cc0ef7e ScrollView.init(_:showsIndicators:content:) + 100
15  ProfileSummary.1.preview-thunk.dylib	       0x10f217bd0 ProfileSummary.__preview__body.getter + 336 (ProfileSummary.swift:15)
16  Landmarks                     	       0x10cf74ff0 ProfileSummary.body.getter + 112 (ProfileSummary.swift:14)
17  Landmarks                     	       0x10cf78689 protocol witness for View.body.getter in conformance ProfileSummary + 9
18  SwiftUI                       	    0x7fff5c94de4c partial apply for closure #1 in ViewBodyAccessor.updateBody(of:changed:) + 22
19  SwiftUI                       	    0x7fff5ce5d525 closure #1 in BodyAccessor.setBody(_:) + 34
20  SwiftUI                       	    0x7fff5c94b981 ViewBodyAccessor.updateBody(of:changed:) + 1310
21  SwiftUI                       	    0x7fff5ce5dab5 DynamicBody.updateValue() + 428
22  SwiftUI                       	    0x7fff5d04f428 partial apply for implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 26
23  AttributeGraph                	    0x7fff4ff9b37b AG::Graph::UpdateStack::update() + 553
24  AttributeGraph                	    0x7fff4ff9b971 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 411
25  AttributeGraph                	    0x7fff4ffa1d18 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 554
26  AttributeGraph                	    0x7fff4ffb8729 AGGraphGetValue + 210
27  SwiftUI                       	    0x7fff5ce64ad2 AnyViewList.updateValue() + 44
28  SwiftUI                       	    0x7fff5c83af29 partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 15
29  AttributeGraph                	    0x7fff4ff9b37b AG::Graph::UpdateStack::update() + 553
30  AttributeGraph                	    0x7fff4ff9b971 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 411
31  AttributeGraph                	    0x7fff4ffa1d18 AG::Graph::input_value_ref_slow(AG::data::ptr<AG::Node>, AG::AttributeID, unsigned int, AGSwiftMetadata const*, unsigned char&, long) + 554
32  AttributeGraph                	    0x7fff4ffb8729 AGGraphGetValue + 210
33  SwiftUI                       	    0x7fff5ce65382 AnyViewList.Item.list.getter + 54
34  SwiftUI                       	    0x7fff5ce64cdb AnyViewList.updateValue() + 565
35  SwiftUI                       	    0x7fff5c83af29 partial apply for specialized implicit closure #2 in implicit closure #1 in closure #1 in closure #1 in Attribute.init<A>(_:) + 15
36  AttributeGraph                	    0x7fff4ff9b37b AG::Graph::UpdateStack::update() + 553
37  AttributeGraph                	    0x7fff4ff9b971 AG::Graph::update_attribute(AG::data::ptr<AG::Node>, unsigned int) + 411
38  AttributeGraph                	    0x7fff4ffa1656 AG::Graph::value_ref(AG::AttributeID, AGSwiftMetadata const*, unsigned char&) + 128
39  AttributeGraph                	    0x7fff4ffb877a AGGraphGetValue + 291
40  SwiftUI                       	    0x7fff5c8b67bd TestableViewContent.list.getter + 47
41  SwiftUI                       	    0x7fff5c8b64a6 TestableViewContent.children.getter + 41
42  SwiftUI                       	    0x7fff5c8b61f2 View.withChildrenForTest<A>(environment:options:body:) + 181
43  SwiftUI                       	    0x7fff5cc7de3f View._allPreviews.getter + 88
44  SwiftUI                       	    0x7fff5cc7ddbe MakePreviews.visit<A>(type:) + 128
45  SwiftUI                       	    0x7fff5d1a5afc _callVisitViewType2 + 34
46  SwiftUI                       	    0x7fff5cc7dd18 static _PreviewProvider._allPreviews.getter + 165
47  PreviewsInjection             	       0x10d45ef6f 0x10d43b000 + 147311
48  PreviewsInjection             	       0x10d45eea3 0x10d43b000 + 147107
49  PreviewsInjection             	       0x10d4574b6 0x10d43b000 + 115894
50  PreviewsInjection             	       0x10d45385d 0x10d43b000 + 100445
51  PreviewsInjection             	       0x10d453265 0x10d43b000 + 98917
52  PreviewsInjection             	       0x10d453519 0x10d43b000 + 99609
53  BoardServices                 	    0x7fff261a2c1c +[BSXPCServiceConnectionProxy invokeMethod:onTarget:withMessage:forConnection:] + 1596
54  BoardServices                 	    0x7fff261b0884 __63-[BSXPCServiceConnectionEventHandler connection:handleMessage:]_block_invoke + 562
55  BoardServices                 	    0x7fff261d9879 BSXPCServiceConnectionExecuteCallOut + 325
56  BoardServices                 	    0x7fff261b04cf -[BSXPCServiceConnectionEventHandler connection:handleMessage:] + 172
57  BoardServices                 	    0x7fff261d8bd7 -[BSXPCServiceConnection _connection_handleMessage:fromPeer:withHandoff:] + 679
58  libdispatch.dylib             	    0x7fff2011265a _dispatch_call_block_and_release + 12
59  libdispatch.dylib             	    0x7fff2011383a _dispatch_client_callout + 8
60  libdispatch.dylib             	    0x7fff20119e0c _dispatch_lane_serial_drain + 743
61  libdispatch.dylib             	    0x7fff2011aa07 _dispatch_lane_invoke + 455
62  libdispatch.dylib             	    0x7fff20120ba5 _dispatch_main_queue_callback_4CF + 848
63  CoreFoundation                	    0x7fff2036c84d __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
64  CoreFoundation                	    0x7fff203670aa __CFRunLoopRun + 2772
65  CoreFoundation                	    0x7fff203660f3 CFRunLoopRunSpecific + 567
66  GraphicsServices              	    0x7fff2c995cd3 GSEventRunModal + 139
67  UIKitCore                     	    0x7fff25059f42 -[UIApplication _run] + 928
68  UIKitCore                     	    0x7fff2505eb5e UIApplicationMain + 101
69  SwiftUI                       	    0x7fff5d0d5dcf closure #1 in KitRendererCommon(_:) + 196
70  SwiftUI                       	    0x7fff5d0d5d09 runApp<A>(_:) + 148
71  SwiftUI                       	    0x7fff5cabdbad static App.main() + 61
72  Landmarks                     	       0x10cf6e078 static LandmarksApp.$main() + 72 (LandmarksApp.swift:10)
73  Landmarks                     	       0x10cf6e153 main + 67
74  dyld_sim                      	       0x10d2e0ee9 start_sim + 10
75  dyld                          	       0x11c9d14fe start + 462

Thread 1:
0   libsystem_pthread.dylib       	    0x7fff6da2dfec start_wqthread + 0

Thread 2:
0   libsystem_pthread.dylib       	    0x7fff6da2dfec start_wqthread + 0

Thread 3:
0   libsystem_pthread.dylib       	    0x7fff6da2dfec start_wqthread + 0

Thread 4:
0   libsystem_pthread.dylib       	    0x7fff6da2dfec start_wqthread + 0

Thread 5:: com.apple.uikit.eventfetch-thread
0   libsystem_kernel.dylib        	    0x7fff6d9d4aba mach_msg_trap + 10
1   libsystem_kernel.dylib        	    0x7fff6d9d4e2b mach_msg + 59
2   CoreFoundation                	    0x7fff2036c440 __CFRunLoopServiceMachPort + 319
3   CoreFoundation                	    0x7fff20366ae3 __CFRunLoopRun + 1293
4   CoreFoundation                	    0x7fff203660f3 CFRunLoopRunSpecific + 567
5   Foundation                    	    0x7fff2081c754 -[NSRunLoop(NSRunLoop) runMode:beforeDate:] + 213
6   Foundation                    	    0x7fff2081c9cb -[NSRunLoop(NSRunLoop) runUntilDate:] + 72
7   UIKitCore                     	    0x7fff2511a2d1 -[UIEventFetcher threadMain] + 491
8   Foundation                    	    0x7fff20845550 __NSThread__start__ + 1025
9   libsystem_pthread.dylib       	    0x7fff6da324f4 _pthread_start + 125
10  libsystem_pthread.dylib       	    0x7fff6da2e00f thread_start + 15


Thread 0 crashed with X86 Thread State (64-bit):
  rax: 0x0000000200000003  rbx: 0x000000000000000b  rcx: 0xfffffffe00000000  rdx: 0x0000000000000003
  rdi: 0x00007fc1b97183b8  rsi: 0x0000000000000103  rbp: 0x00007ff7b2fa4fb0  rsp: 0x00007ff7b2fa4f20
   r8: 0x0000000000000154   r9: 0x0000000000000013  r10: 0x00007fc1ba300000  r11: 0x00007fc1ba3064c0
  r12: 0x00007fff5d2ff970  r13: 0x0000000000000083  r14: 0x00007fff5d2e7980  r15: 0x00007fc1b97183b0
  rip: 0x00007fff3073ff1b  rfl: 0x0000000000010246  cr2: 0x00007fff200b3b1d
  
Logical CPU:     6
Error Code:      0x00000000 
Trap Number:     6


Binary Images:
    0x7fff30710000 -     0x7fff30acffff libswiftCore.dylib (*) <6013ecdd-c54d-3948-8b7c-e9f5613107f6> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/swift/libswiftCore.dylib
    0x7fff5c697000 -     0x7fff5d4f2fff com.apple.SwiftUI (3.2.5.0.100) <c680d16f-a317-3c6e-87fa-141c22a715da> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/SwiftUI.framework/SwiftUI
       0x10cf53000 -        0x10cfb6fff ICO.Landmarks (1.0) <d223671b-18b5-3c27-aa9c-00792427ac33> /Users/USER/Library/Developer/Xcode/UserData/Previews/Simulator Devices/4DBA156B-3EA5-4245-80BD-56F3BCBDCA93/data/Containers/Bundle/Application/8672C598-D194-4AD1-B640-4AF093632FA2/Landmarks.app/Landmarks
       0x10f213000 -        0x10f21efff ProfileSummary.1.preview-thunk.dylib (*) <53ccd61f-236c-30da-a236-69bb98954b8a> /Users/USER/Library/Developer/Xcode/DerivedData/Landmarks-ggpnlakhqjqzlsaruntpdkhagabp/Build/Intermediates.noindex/Previews/Landmarks/Intermediates.noindex/Landmarks.build/Debug-iphonesimulator/Landmarks.build/Objects-normal/x86_64/ProfileSummary.1.preview-thunk.dylib
    0x7fff4ff96000 -     0x7fff4ffcafff com.apple.AttributeGraph (3.2.1) <20c3feeb-4d9b-3501-be47-de9ae60ae297> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/AttributeGraph.framework/AttributeGraph
       0x10d43b000 -        0x10d46cfff com.apple.dt.PreviewsInjection (13.0) <385f7334-89cd-3998-bc1b-54dd898b38ad> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/PreviewsInjection.framework/PreviewsInjection
    0x7fff2619e000 -     0x7fff261edfff com.apple.BoardServices (1.0) <d4742ff1-1051-348e-afb4-dcb3bc434c50> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/BoardServices.framework/BoardServices
    0x7fff20110000 -     0x7fff20157fff libdispatch.dylib (*) <20999e62-bcc2-377e-8ce7-afda271eb914> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/system/libdispatch.dylib
    0x7fff202ec000 -     0x7fff2068bfff com.apple.CoreFoundation (6.9) <c6308add-135b-3a70-9b81-1ce4d994c215> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/CoreFoundation.framework/CoreFoundation
    0x7fff2c992000 -     0x7fff2c99afff com.apple.GraphicsServices (1.0) <adea8ae1-c723-351b-ac2e-22fd2c861c30> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/GraphicsServices.framework/GraphicsServices
    0x7fff2438c000 -     0x7fff25934fff com.apple.UIKitCore (1.0) <3ef2c381-8193-3e3c-801f-925bd1050bd7> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/PrivateFrameworks/UIKitCore.framework/UIKitCore
       0x10d2df000 -        0x10d32cfff dyld_sim (*) <3f15c197-2d43-3490-98ff-de85f18dadf2> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/usr/lib/dyld_sim
       0x11c9cc000 -        0x11ca37fff dyld (*) <7de33963-bbc5-3996-ba6e-f1d562c17c95> /usr/lib/dyld
    0x7fff6da2c000 -     0x7fff6da37fff libsystem_pthread.dylib (*) <ee564342-d8f2-396d-b642-40092cf34d82> /usr/lib/system/libsystem_pthread.dylib
    0x7fff6d9d4000 -     0x7fff6da0afff libsystem_kernel.dylib (*) <c1d58a50-5a4d-3bcb-a1fc-ec0902ce34d3> /usr/lib/system/libsystem_kernel.dylib
    0x7fff206fa000 -     0x7fff209b0fff com.apple.Foundation (6.9) <61dcc68e-1236-3624-bb6d-a3a850adf63a> /Applications/Xcode.app/Contents/Developer/Platforms/iPhoneOS.platform/Library/Developer/CoreSimulator/Profiles/Runtimes/iOS.simruntime/Contents/Resources/RuntimeRoot/System/Library/Frameworks/Foundation.framework/Foundation

EOF

^ That is the translated report. If you guys would like the full report I will post it. I am stuck and don't know what to do any help will be appreciated!

Answered by Claude31 in 705006022

Could you show the complete code, problem may come from elsewhere. Notably the Preview file. And tell where you get the crash.

Preview struct should be like this:

struct LandmarkDetail_Previews: PreviewProvider {
static let modelData = ModelData()
static var previews: some View {
LandmarkDetail(landmark: modelData.landmarks[0])
.environmentObject(modelData)
}
}

It could be you forgot the .environmentObject(modelData)

Note: next time, choose more appropriate tags. That's not an iOS issue, nor Swift, nor Xcode Server but definitely a SwiftUI. That will help you get more and better answers.

Accepted Answer

Could you show the complete code, problem may come from elsewhere. Notably the Preview file. And tell where you get the crash.

Preview struct should be like this:

struct LandmarkDetail_Previews: PreviewProvider {
static let modelData = ModelData()
static var previews: some View {
LandmarkDetail(landmark: modelData.landmarks[0])
.environmentObject(modelData)
}
}

It could be you forgot the .environmentObject(modelData)

Note: next time, choose more appropriate tags. That's not an iOS issue, nor Swift, nor Xcode Server but definitely a SwiftUI. That will help you get more and better answers.

Repeated Crash Reports when I add code to a project
 
 
Q