XCode + Swift: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee99cdfd8)

Viewed 1241

I've seen a few posts on here about the same Thread 1 warning, however nothing that's specific to my code (or at least not that I can see).

The error is: Thread 1: EXC_BAD_ACCESS (code=2, address=0x7ffee99cdfd8)

The issue is: neither the XCode builder or preview are working, making it quite impossible to build. The @main is where the error crops up in the App view.

import SwiftUI

@main
struct WApp: App {
    var body: some Scene {
        WindowGroup {
            ContentView()
        }
    }
}

Here is the content view code:

import SwiftUI

struct ContentView: View {
    var body: some View {
        ZStack {
            LinearGradient(gradient:  Gradient(colors: [.blue, .white]), startPoint: .topLeading, endPoint: .bottomTrailing)
                .edgesIgnoringSafeArea(.all)
            
            VStack {
                Text("California")
                    .font(.system(size: 32, weight: .medium, design: .default))
                    .foregroundColor(.white)
                    .padding()
                
                VStack(spacing:7) {
                Image(systemName: "cloud.sun.fill")
                    .renderingMode(.original)
                    .resizable()
                    .aspectRatio(contentMode: .fit)
                    .frame(width:180, height:180)
                
                Text("76°")
                    .font(.system(size:70, weight: .medium))
                    .foregroundColor(.white)
            }
                HStack{
                    VStack(spacing:7) {
                    Text("Tuesday")
                            .font(.system(size: 20, weight: .medium, design: .default))
                            .foregroundColor(.white)
                            
                    Image(systemName: "cloud.sun.fill")
                            .renderingMode(.original)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        frame(width:40, height:40)
                    
                    Text("74°")
                            .font(.system(size: 20, weight: .medium, design: .default))
                            .foregroundColor(.white)
                            
                }
                }
                
                Spacer()
                
                
            
            }
        }
        
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Any help would be great. I've tried refactoring in case the length of the code is the issue, which hasn't worked. Thanks.

1 Answers

It's hard to believe but this crash is caused by a typo, which produces valid (i.e. "compilable") code, but it then falls into some kind of infinite loop and runs out of memory perhaps?:

Image(systemName: "cloud.sun.fill")
                            .renderingMode(.original)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                        frame(width:40, height:40) // <-- missing dot!

so you should change it to:

Image(systemName: "cloud.sun.fill")
                            .renderingMode(.original)
                            .resizable()
                            .aspectRatio(contentMode: .fit)
                            .frame(width:40, height:40)

Edit

couldn't resist adding this that just popped in my head:

For the want of a dot the app was lost
For the want of an app the dev was lost
For the want of a dev the business was lost
For the want of a business the market was lost
For the want of a market the country was lost

Related