WWDC22 SALE: Save 50% on all my Swift books and bundles! >>

Why the app is crashing when tab change (EXC_BAD_ACCESS)?

Forums > SwiftUI

@m-kul  

I have a custom tabbiew in my app. It works fine until I go to a subview of a tab. For example; from the Feed tab I click to Messages tab, then click to see the contents of a chat, then I click Feed tab again and crashes immediately.

struct ContentView: View {

    //.....

    @StateObject var viewRouter = ViewRouter()

    var body: some View {
        GeometryReader { geometry in
            VStack {
                Spacer()
                switch viewRouter.currentPage {
                case .feed:
                    NavigationView {
                        FeedView(posts: feedPosts, title: "Feed")
                    }.navigationViewStyle(.stack)
                case .browse:
                    NavigationView {
                        BrowseView(posts: browsePosts)
                    }
                    .navigationViewStyle(.stack)
                case .messages:
                    NavigationView {
                        ChatListView(newMessage: $newMessageAvailable)
                    }
                    .navigationViewStyle(.stack)
      //......

struct ChatListView: View {

//....

var body: some View {
    ScrollViewReader { proxy in
        ScrollView {
            if (!gettingMessages) {
                if (commentArray.count > 0) {
                    VStack{
                        ForEach(commentArray, id: \.chatID) { comment in
                            NavigationLink(destination: ChatDetailView(keepViewOpen: self.$keepChatViewOpen, recipientID: getRecipientID(comment: comment), recipientName: getRecipientName(comment: comment)), isActive: self.$keepChatViewOpen
                                           , label: {
                                ChatListItem(comment: comment, recipientName: getRecipientName(comment: comment), userID: getRecipientID(comment: comment), read: comment.read)
                            })

                        }
                    }

There is nothing fancy in ChatDetailView and the crash happens for every kind of view provided that it is a view navigated from any other view with navigation link. The error is as follows: Thread 1: EXC_BAD_ACCESS (code=1, address=0xf2010803e530)

   

I have ran into the exact same problem, the crash disappears if I optimize the build for speed but keeps happening if not optimized. Very strange issue

   

Most likely this kind of error happens when you try to access memory in an invalid way. F.e. accessing a variable which is nil.

@m-kul I would suggest you use SwiftUI's TabView for your tab view which should reduce your code and make it easier. But from the provided code I'm not able to give a proper advice.

1      

Hacking with Swift is sponsored by Emerge

SPONSORED Why are Swift reference types bad for app startup time, and what’s the performance cost of protocol conformances? That’s just a couple of the topics you can learn about on the Emerge blog — written by the app performance experts behind Emerge’s advanced app optimization and monitoring tools, based on their experience of working at companies like Apple, Airbnb, Snap, and Spotify.

Find out more

Sponsor Hacking with Swift and reach the world's largest Swift community!

Reply to this topic…

You need to create an account or log in to reply.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.