Register user into app in SwiftUI using Firebase

I am making app which one of functionality is register user with firebase, but sometime when I register user I got app crash but user is added into db. I am getting error like this: EXC_BAD_ACCESS (code=2, address=0x2600000000) or adres = 0x10 which mean the is some memory leak but when I use instrumental leak every thing is ok.

Second weird thing is that when I use instrumental leak I am not getting error (I added something like 40 users) but when I close instrumental and rebuilt I got crash in max 3 attempts.

//  FormComponent.swift
//  SpaceManager
//
//  Created by Kuba Kromomołowski on 17/04/2024.
//

import Foundation
import SwiftUI

struct FormComponent: View {
    var isRegister: Bool = true
    @State private var repeatedPassword: String = ""
    @StateObject private var loginHandler = LoginViewModel()
    @StateObject private var registerHandler = RegisterViewModel()
    @EnvironmentObject var permissionViewModel: PermissionViewModel
    var body: some View {
        Form {
            TextField("Email", text: isRegister ? $registerHandler.email : $loginHandler.email)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .font(.system(size: 25))
                .multilineTextAlignment(.center)
                .autocapitalization(.none)
                .disableAutocorrection(true)
            SecureField("Hasło", text: isRegister ? $registerHandler.password : $loginHandler.password)
                .textFieldStyle(RoundedBorderTextFieldStyle())
                .font(.system(size: 25))
                .multilineTextAlignment(.center)
                .autocapitalization(.none)
                .disableAutocorrection(true)
            if (isRegister) {
                SecureField("Powtórz haslo", text: $registerHandler.repeatedPassword)
                    .textFieldStyle(RoundedBorderTextFieldStyle())
                    .font(.system(size: 25))
                    .multilineTextAlignment(.center)
                    .autocapitalization(.none)
                    .disableAutocorrection(true)
            }
            BtnClearComponet(btnText: isRegister ? "Zarejestruj się" : "Zaloguj się",
                                       btnRegister: isRegister,
                                       action: {
                                          if isRegister {
                                              registerHandler.registerUser() {
                                                  permissionViewModel.getPermission()
                                              }
                                          } else {
                                              loginHandler.userLogin() {
                                                  permissionViewModel.getPermission()
                                              }
                                          }
                                       },
                                       loginHandler: loginHandler,
                                       registerHandler: registerHandler
            )
                .padding(.bottom, 5)
        }
        .frame(width:350,height:400)
        .scrollContentBackground(.hidden)
        .padding(.top, 50)
    }
}


//
//  RegisterViewModel.swift
//  SpaceManager
//
//  Created by Kuba Kromomołowski on 03/05/2024.
//

import Foundation
import Firebase
import FirebaseAuth
import FirebaseFirestore

class RegisterViewModel : ObservableObject {
    @Published var email: String = ""
    @Published var password: String = ""
    @Published var repeatedPassword: String = ""
    
    @Published var isFail: Bool = false
    @Published var message: String = ""
    

    func registerUser(completion: @escaping () -> Void) {
        if(!validInput()){
            return
        }
        Auth.auth().createUser(withEmail: email, password: password) { [weak self] res, 
err in
            guard let self = self else { return }
            if let err = err {
                self.isFail = true
                self.message = "Błąd przy rejestracji \(err.localizedDescription)"
                return
            }
            guard let userID = res?.user.uid else {
                return
            }
//            DispatchQueue.main.async {
               // print("Starting adding...")
                self.addIntoDatabe(userID: userID, email: self.email)
//            }
            completion()
        }
    }
 
    private func addIntoDatabe(userID: String, email: String) {
        let newUser = User(uid: userID, email: email, permission: Permission.Admin, itemReads: [["Prop":2]],
                           numberOfAddedItem: 0, numberOfReadItem: 0)
        let db = Firestore.firestore()

        db.collection("users")
            .document(userID)
            .setData(["uid": newUser.uid, "email": newUser.email, "permission": newUser.permission.rawValue, "itemReads": newUser.itemReads, "numberOfAddedItem": newUser.numberOfAddedItem, "numberOfReadItem": newUser.numberOfReadItem])
        print("User has been added into db")
    }
}
Answered by DTS Engineer in 812957022

I am getting error like this: EXC_BAD_ACCESS (code=2, address=0x2600000000) or adres = 0x10 which mean the is some memory leak but when I use instrumental leak every thing is ok.

A memory leak won't cause a crash with EXC_BAD_ACCESS in the crash signature, so that isn't the right debugging strategy to apply here. Based on this very small fragment of information from the crash report, you should read Investigating memory access crashes to understand the correct debugging strategy to apply here. One of the most important next steps for you is to use the sanitizers, as mentioned by that article as well as well as by Diagnosing memory, thread, and crash issues early.

— Ed Ford,  DTS Engineer

I am getting error like this: EXC_BAD_ACCESS (code=2, address=0x2600000000) or adres = 0x10 which mean the is some memory leak but when I use instrumental leak every thing is ok.

A memory leak won't cause a crash with EXC_BAD_ACCESS in the crash signature, so that isn't the right debugging strategy to apply here. Based on this very small fragment of information from the crash report, you should read Investigating memory access crashes to understand the correct debugging strategy to apply here. One of the most important next steps for you is to use the sanitizers, as mentioned by that article as well as well as by Diagnosing memory, thread, and crash issues early.

— Ed Ford,  DTS Engineer

Register user into app in SwiftUI using Firebase
 
 
Q