Skip to content

Instantly share code, notes, and snippets.

@ryanlintott
Last active September 15, 2024 02:05
Show Gist options
  • Save ryanlintott/e9accf235f4afb49f6178bec73bf5f80 to your computer and use it in GitHub Desktop.
Save ryanlintott/e9accf235f4afb49f6178bec73bf5f80 to your computer and use it in GitHub Desktop.
FB14810097: App crashes when an item with an alignmentGuide is removed from ForEach inside a Layout with animation.
//
// CrashingView.swift
// CrashRemovingItem
//
// Created by Ryan Lintott on 2024-08-13.
//
import SwiftUI
struct CrashingView: View {
@State private var items: [String] = Array(1...10).map { String("\($0)") }
var body: some View {
VStack {
HStack {
ForEach(items, id: \.self) { item in
Text(item)
.alignmentGuide(HorizontalAlignment.center) { d in
0
}
}
}
/// 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:

  • Xcode 16 beta 1-6 on iOS 18 or Xcode 16.1 beta with iOS 18.1
  • Swift 6 language mode
  • HStack (or any other Layout) with a ForEach showing items with an alignmentGuide view modifier
  • An animation modifier layout animating when the array of items changes

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 betas 1, 2, 4, and 5 and iOS 18.1 beta 1 and on an iPhone 11 device running iOS 18.0 (22A5338b)

@ryanlintott
Copy link
Author

Seems like it's a bug with alignmentGuide. Here's a similar crash: https://developer.apple.com/forums/thread/760645?login=true

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