UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

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)

1      

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

1      

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.

2      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

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

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

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.