Search code examples
iosswiftobjective-calamofirecrashlytics

Alamofire crash in session.swift deinit:


I am working on the Swift project and I have used the alamofire for handling the network. This crash only occurred in 14.6.0 and above the OS versions. I have attached the code below for creating/cancel session:

Session create:

func sessionManager() -> Session {   
    //SSL pining
        instantiateTrustKit()
        if let manage = self.manager {
            return manage
        }
        else {
            let configuration = URLSessionConfiguration.default
            configuration.timeoutIntervalForRequest = 30
            self.manager = Session(configuration: configuration, delegate: CustomDelegate())
            guard let sessionManager = manager else {
                return Session.default
            }
            return sessionManager
        }
    }
    

Cancel ongoing request:

func cancelAllRequests() {
        sessionManager().session.getTasksWithCompletionHandler { dataTasks, uploadTasks, downloadTasks in
            dataTasks.forEach {
                $0.cancel() }
            uploadTasks.forEach {
                $0.cancel() }
            downloadTasks.forEach {
                $0.cancel() }
        }
}



Crash Stack trace:
    Crashed: com.apple.root.background-qos
0  libobjc.A.dylib                0x25dd0 objc_release + 16
1  Alamofire                      0xb4b90 Session.deinit + 204 (Session.swift:204)
2  Alamofire                      0xb4c2c Session.__deallocating_deinit + 202 (Session.swift:202)
3  libswiftCore.dylib             0x383c98 _swift_release_dealloc + 40
4  libswiftCore.dylib             0x384b90 bool swift::HeapObjectSideTableEntry::decrementStrong<(swift::PerformDeinit)1>(unsigned int) + 288
  1. While sending requests we called the sessionManager function to get the current session.
  2. When the user opens the app for the first time and the user navigates to the main screen the cancel request function will be called.

Could anyone know the above issue?


Solution

  • If you go to Xcode menu “Product” » “Scheme” » “Edit Scheme...” » “Run” » “Diagnostics” and turn on “Address Sanitizer” and the “Detect use of stack after return”, it will help you identify and resolve these issues in the future:

    enter image description here