Crash embedding UIView in SwiftUI

I'm getting the following crash using my `UIView` subclass with `UIViewRepresentable`:


(lldb) bt
* thread #1, queue = 'com.apple.main-thread', stop reason = EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)
    frame #0: 0x00007fff2c7cd373 SwiftUI`(extension in SwiftUI):__C.NSAttributedString.(MetricsCache in _4EAA3873E044FE8466A2EF8771E1058D).findMetrics(requestedSize: __C.CGSize) -> Swift.Optional<(extension in SwiftUI):__C.NSAttributedString.(Metrics in _4EAA3873E044FE8466A2EF8771E1058D)> + 1059
    frame #1: 0x00007fff2c7ca659 SwiftUI`(extension in SwiftUI):__C.NSAttributedString.(MetricsCache in _4EAA3873E044FE8466A2EF8771E1058D).metrics(requestedSize: __C.CGSize) -> (extension in SwiftUI):__C.NSAttributedString.(Metrics in _4EAA3873E044FE8466A2EF8771E1058D) + 297
    frame #2: 0x00007fff2c7ca506 SwiftUI`SwiftUI.StyledTextLayoutDelegate.sizeThatFits(SwiftUI._ProposedSize) -> __C.CGSize + 118
    frame #3: 0x00007fff2c6c0b89 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).sizeChildrenGenerallyWithConcreteMajorProposal(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 1273
    frame #4: 0x00007fff2c6c2550 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren1(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 144
    frame #5: 0x00007fff2c6be7d0 SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> () + 272
    frame #6: 0x00007fff2c6bdb3f SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).sizeThatFits(SwiftUI._ProposedSize) -> __C.CGSize + 431
    frame #7: 0x00007fff2c6bd986 SwiftUI`SwiftUI.StackLayout.(Storage in _68D684484B5AEF917B6B8353D57CF590).sizeThatFits(SwiftUI._ProposedSize) -> __C.CGSize + 70
    frame #8: 0x00007fff2c6c0b89 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).sizeChildrenGenerallyWithConcreteMajorProposal(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 1273
    frame #9: 0x00007fff2c6c2550 SwiftUI`function signature specialization <arg[1] =="" [closure="" propagated="" :="" closure="" #1="" (swiftui.stacklayout.(child="" in="" _68d684484b5aef917b6b8353d57cf590))="" -=""> Swift.Optional in SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> (), Argument Types : [Swift.Optional]> of SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren1(in: SwiftUI._ProposedSize, minorProposalForChild: (SwiftUI.StackLayout.(Child in _68D684484B5AEF917B6B8353D57CF590)) -> Swift.Optional) -> () + 144
    frame #10: 0x00007fff2c6be7d0 SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).placeChildren(in: SwiftUI._ProposedSize) -> () + 272
    frame #11: 0x00007fff2c6bdca3 SwiftUI`SwiftUI.StackLayout.(UnmanagedImplementation in _68D684484B5AEF917B6B8353D57CF590).childGeometries(at: __C.CGSize) -> Swift.Array + 83
    frame #12: 0x00007fff2c6d8dca SwiftUI`generic specialization <swiftui.layoutcontext.(childgeometriesquery in="" _b0b50b213a0802e1084335d0ad6cd036)<swiftui._formvstacklayout="">> of (extension in SwiftUI):SwiftUI.LayoutQuery< where A.Inputs == AttributeGraph.List2<__C.CGSize, SwiftUI.LayoutComputer>, A.Value == Swift.Array, A.Layout.PlacementContext == SwiftUI.LayoutContext>.update(context: inout AttributeGraph.AttributeContext) -> () + 122
    frame #13: 0x00007fff2c5493aa SwiftUI`merged partial apply forwarder for protocol witness for static AttributeGraph.UntypedAttribute._update(_: Swift.UnsafeMutableRawPointer, graph: __C.AGGraphRef, attribute: __C.AGAttribute) -> () in conformance SwiftUI.(DynamicLayoutViewChildGeometry in _8B9C7F39264416187A895085215951BC) : AttributeGraph.UntypedAttribute in SwiftUI + 26
    frame #14: 0x00007fff2fc2ed45 AttributeGraph`AG::Graph::UpdateStack::update() + 455
    frame #15: 0x00007fff2fc2f253 AttributeGraph`AG::Graph::update_attribute(unsigned int, bool) + 373
    frame #16: 0x00007fff2fc33d5b AttributeGraph`AG::Subgraph::update(unsigned int) + 729
    frame #17: 0x00007fff2c51d690 SwiftUI`SwiftUI.ViewGraph.(runTransaction in _D63C4EB7F2B205694B6515509E76E98B)(in: __C.AGGraphRef) -> () + 224
    frame #18: 0x00007fff2c51d446 SwiftUI`closure #1 (__C.AGGraphRef) -> () in SwiftUI.ViewGraph.flushTransactions() -> () + 262
    frame #19: 0x00007fff2c51d125 SwiftUI`SwiftUI.ViewGraph.flushTransactions() -> () + 213
    frame #20: 0x00007fff2c51d26f SwiftUI`closure #1 (SwiftUI.ViewGraph) -> () in closure #1 () -> () in SwiftUI.ViewGraph.asyncTransaction(_: SwiftUI.Transaction, mutation: A, style: SwiftUI._GraphMutation_Style) -> () + 15
    frame #21: 0x00007fff2c51b539 SwiftUI`function signature specialization <arg[2] =="" [constant="" propagated="" function="" :="" closure="" #1="" (swiftui.viewgraph)="" -=""> () in closure #1 () -> () in SwiftUI.ViewGraph.asyncTransaction(_: SwiftUI.Transaction, mutation: A, style: SwiftUI._GraphMutation_Style) -> ()]> of reabstraction thunk helper from @callee_guaranteed (@guaranteed SwiftUI.ViewGraph) -> () to @escaping @callee_guaranteed (@guaranteed SwiftUI.ViewGraph) -> (@out ()) + 9
    frame #22: 0x00007fff2c7f5ab7 SwiftUI`(extension in SwiftUI):SwiftUI.ViewRendererHost.updateViewGraph(body: (SwiftUI.ViewGraph) -> A1) -> A1 + 71
    frame #23: 0x00007fff2c7f57b3 SwiftUI`protocol witness for SwiftUI.ViewGraphDelegate.updateViewGraph(body: (SwiftUI.ViewGraph) -> A1) -> A1 in conformance SwiftUI._UIHostingView : SwiftUI.ViewGraphDelegate in SwiftUI + 19
    frame #24: 0x00007fff2c51d24a SwiftUI`closure #1 () -> () in SwiftUI.ViewGraph.asyncTransaction(_: SwiftUI.Transaction, mutation: A, style: SwiftUI._GraphMutation_Style) -> () + 122
    frame #25: 0x00007fff2c53977c SwiftUI`reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_guaranteed () -> (@out ()) + 12
    frame #26: 0x00007fff2c3a1401 SwiftUI`partial apply forwarder for reabstraction thunk helper from @escaping @callee_guaranteed () -> () to @escaping @callee_guaranteed () -> (@out ()) + 17
    frame #27: 0x00007fff2c3a1327 SwiftUI`static (extension in SwiftUI):__C.NSRunLoop.flushObservers() -> () + 119
    frame #28: 0x00007fff2c3a12a9 SwiftUI`closure #1 (Swift.Optional<__C.CFRunLoopObserverRef>, __C.CFRunLoopActivity, Swift.Optional) -> () in static (extension in SwiftUI):__C.NSRunLoop.addObserver(() -> ()) -> () + 9
    frame #29: 0x00007fff2c3a139b SwiftUI`@objc closure #1 (Swift.Optional<__C.CFRunLoopObserverRef>, __C.CFRunLoopActivity, Swift.Optional) -> () in static (extension in SwiftUI):__C.NSRunLoop.addObserver(() -> ()) -> () + 43
    frame #30: 0x00007fff23d9d2a7 CoreFoundation`__CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 23
    frame #31: 0x00007fff23d97d5e CoreFoundation`__CFRunLoopDoObservers + 430
    frame #32: 0x00007fff23d982aa CoreFoundation`__CFRunLoopRun + 1226
    frame #33: 0x00007fff23d97ac4 CoreFoundation`CFRunLoopRunSpecific + 404
    frame #34: 0x00007fff38b2fc1a GraphicsServices`GSEventRunModal + 139
    frame #35: 0x00007fff48bc7f80 UIKitCore`UIApplicationMain + 1605
  * frame #36: 0x0000000101e9796b TestApp`main at AppDelegate.swift:9:7
    frame #37: 0x00007fff519521fd libdyld.dylib`start + 1
(lldb)


Seems like its a crash in `NSAttributedString.MetricsCache` - any idea what this is?

I'm on Xcode 11.4

Replies

Can you show a code enough to reproduce the issue?

I'm having a similar issue. Any resolution?