Skip to content

Instantly share code, notes, and snippets.

@ryanlintott
Last active August 15, 2024 12:45
Show Gist options
  • Save ryanlintott/aebd37d2542e8ba49954339892411aaa to your computer and use it in GitHub Desktop.
Save ryanlintott/aebd37d2542e8ba49954339892411aaa to your computer and use it in GitHub Desktop.
FB14787915: VariadicView crashes when an item is removed if the child view has an alignmentGuide and the change is animated.
//
// CrashingView.swift
// CrashRemovingItem
//
// Created by Ryan Lintott on 2024-08-13.
//
import SwiftUI
fileprivate struct _VariadicRoot: _VariadicView_MultiViewRoot {
@ViewBuilder
public func body(children: _VariadicView.Children) -> some View {
ForEach(children) { child in
child
/// Comment out this alignment guide and removing an item will not crash the app.his alignment guide crashes the app when an item is removed.
.alignmentGuide(HorizontalAlignment.center) { d in
d[HorizontalAlignment.center]
}
}
}
}
struct CrashingView: View {
@State private var items: [String] = Array(1...10).map { String("\($0)") }
var body: some View {
VStack {
ZStack {
_VariadicView.Tree(_VariadicRoot()) {
ForEach(items, id: \.self) { item in
Text(item)
}
}
}
/// Comment out the animation and removing an item will not crash the app.
.animation(.default, value: items)
HStack {
/// Tap this button to remove an item and crash the app
/// It will also log the following
/// `void * _Nullable NSMapGet(NSMapTable * _Nonnull, const void * _Nullable): map table argument is NULL`
Button("Remove Item") {
removeLastItem()
}
.padding()
}
.padding()
}
}
func removeLastItem() {
if !items.isEmpty {
items.removeLast()
}
}
}
#Preview {
CrashingView()
}
With the following setup:
- _VariadicView_MultiViewRoot with an alignmentGuide view modifier on a child view
- An animation modifier outside the variadic view animating when the array of items changes
- Running in Swift 6 language mode and iOS beta 5
When an item is removed from the array the app will crash.
An example file is attached to demonstrate the issue. If you comment out the alignmentGuide or the animation view modifiers the app won't crash but it will still log an issue.
I have confirmed the bug on iPhone 15 Pro simulator running iOS 18.0 (22A5326g) and on an iPhone 11 device running iOS 18.0 (22A5338b)
I believe this is a new issue in beta 5 but I am not positive.
@ryanlintott
Copy link
Author

It seems this bug occurs on regular layouts like HStack as well. FB14810097

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment