SwiftUI StateObject crash: “failed to demangle witness for associated type 'Property’”

Hello, we have weird crash in our app that mostly seems to happen right after launch. It is quite rare and so far I haven’t been able to reproduce it (the info below comes from Crashlytics).

The main error message I have is this:

failed to demangle witness for associated type 'Property' in conformance 'SwiftUI.StateObject<AppBlock.QuickBlockActivityViewModel>.(unknown context at $18f34e5b8).Box: DynamicPropertyBox' from mangled name '���yxG' - subject type x does not conform to protocol ObservableObject 

And here is the stack trace:

Crashed: com.apple.main-thread
0  libsystem_kernel.dylib         0x7200 __pthread_kill + 8
1  libsystem_pthread.dylib        0x71ac pthread_kill + 268
2  libsystem_c.dylib              0x20ca0 abort + 180
3  libswiftCore.dylib             0x3d7304 swift::fatalError(unsigned int, char const*, ...) + 134
4  libswiftCore.dylib             0x3d7324 swift::warningv(unsigned int, char const*, char*) + 30
5  libswiftCore.dylib             0x3ee678 swift_getAssociatedConformanceWitnessSlowImpl(swift::TargetWitnessTable<swift::InProcess>*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetMetadata<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*, swift::TargetProtocolRequirement<swift::InProcess> const*) + 2078
6  libswiftCore.dylib             0x3ecb9c swift_getAssociatedTypeWitness + 236
7  SwiftUI                        0x5b838 OUTLINED_FUNCTION_49 + 640
8  SwiftUI                        0xa8d68 OUTLINED_FUNCTION_513 + 16260
9  SwiftUI                        0x58244 OUTLINED_FUNCTION_177 + 10892
10 SwiftUI                        0x95524 OUTLINED_FUNCTION_1160 + 6632

We are using the view model (QuickBlockActivityViewModel) in a SwiftUI view that is part of collection view using the new UIHostingConfiguration from iOS 16.

Our view model is a subclass of view model for older iOS versions that conforms to ObservableObject and is marked as @MainActor.

And the view model is used like this: @StateObject private var viewModel = QuickBlockActivityViewModel()

Internally the view model uses Combine to monitor a couple of states from other parts of the app to modify its properties.

Answered by nemecek_f in 741919022

I tried not having the view model be a subclass which was itself conforming to ObservableObject and so far haven't seen a single crash report from this version.

It required to have some code duplication but seems like it might have worked

Thanks!

The crash we are seeing isn't related to any external dependency which both the solution posted seem to solve.

I am tempted to try changing the "Dead Code Stripping" but I am afraid of other repercusions this could have.

Accepted Answer

I tried not having the view model be a subclass which was itself conforming to ObservableObject and so far haven't seen a single crash report from this version.

It required to have some code duplication but seems like it might have worked

I have experienced the same issue. My code was like:

In side the view:

@StateObject var foo: AbstractFoo
class AbstractFoo: ObservableObject {}
class Foo: AbstractFoo{}

I tried not having the view model be a subclass which was itself conforming to ObservableObject and so far haven't seen a single crash report from this version.

If the above is true, does that mean I'm not supposed to use inheritance for StateObject? Can someone from Apple please confirm?

hi. I have couple crashes of this kind on iOS 15-16 devices. Do you guys experience this issue on iOS 17-18?

SwiftUI StateObject crash: “failed to demangle witness for associated type 'Property’”
 
 
Q