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

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

Forums > SwiftUI


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 {
                switch viewRouter.currentPage {
                case .feed:
                    NavigationView {
                        FeedView(posts: feedPosts, title: "Feed")
                case .browse:
                    NavigationView {
                        BrowseView(posts: browsePosts)
                case .messages:
                    NavigationView {
                        ChatListView(newMessage: $newMessageAvailable)

struct ChatListView: View {


var body: some View {
    ScrollViewReader { proxy in
        ScrollView {
            if (!gettingMessages) {
                if (commentArray.count > 0) {
                        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.


Save 50% in my Black Friday sale.

SAVE 50% To celebrate WWDC22, all our books and bundles are half price, so you can take your Swift knowledge further without spending big! Get the Swift Power Pack to build your iOS career faster, get the Swift Platform Pack to builds apps for macOS, watchOS, and beyond, or get the Swift Plus Pack to learn advanced design patterns, testing skills, and more.

Save 50% on all our books and bundles!

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.