title: Alamofire memory leaks Instruments
body: <p>I am trying to clean my app from memory leaks and I have a few problems understanding this</p>
  
  <p><a href="https://i.stack.imgur.com/O9VcR.png"><img src="https://i.stack.imgur.com/O9VcR.png" alt="enter image description here"></a></p>
  
  <ul>
  <li><p>Why Alamofire function <code>Request.serializeResponseJSON</code> is called 30 seconds after I've launched app: I did not touch anything or navigate anywhere, the screen was static.</p></li>
  <li><p>Why does it leaks?</p></li>
  <li><p>Why does my code leaks?</p></li>
  </ul>
  
  <p>I get same leaks when the screen have loaded.</p>
  
  <p>What I've tried so far:</p>
  
  <ul>
  <li><p>Autoreleasepool;</p></li>
  <li><p>Appending to and initialising arrays in every way that possible;</p></li>
  <li><p>Changing all variable (class, func) to be optional/not
  optional/weak;</p></li>
  <li><p>Initialising classes in <code>UIViewController</code>;</p></li>
  <li><p>Initialising classes in main thread;</p></li>
  <li><p>Searching these problems in the internet.</p></li>
  <li><p>I've found out, using Xcode memory tool, that it is somehow connected with  <code>_ContiguousArrayStorage</code>, but I do not understand how and what is it actually.
  <a href="https://i.stack.imgur.com/abyEB.png"><img src="https://i.stack.imgur.com/abyEB.png" alt="enter image description here"></a></p></li>
  </ul>
  
  <p>I am out of any ideas what is wrong here. Any tips would be much appreciated.</p>
  
  <p>Here is all related code:
  My general API request</p>
  
  <pre><code>public func requestWithLocation(_ httpmethod: Alamofire.HTTPMethod, URL: String, parameters: [String: AnyObject]?, completionHandler: @escaping CompletionHandler) -&gt; (){
      var header: HTTPHeaders = [:]
      var location: [String: Double] = [:]
      let locationManager = CLLocationManager()
  
      if (CLLocationManager.authorizationStatus() == .authorizedWhenInUse
          || CLLocationManager.authorizationStatus() == .authorizedAlways) &amp;&amp;  locationManager.location != nil {
          location = [
              "lon" :  locationManager.location!.coordinate.longitude,
              "lat" :  locationManager.location!.coordinate.latitude
          ]
      }
  
      if User.sharedInstance.token != "" {
          header["Authorization"] = User.sharedInstance.token
      }
  
      var parametersWithLocation = parameters ?? [:]
      parametersWithLocation["location"] = location as AnyObject
  
      Alamofire.request("\(serverAddress)/\(URL)", method: httpmethod, parameters: parametersWithLocation, encoding: JSONEncoding.default, headers: header).validate().responseJSON { response in
          var data: JSON?
  
          if response.result.value != nil {
              data = JSON(response.result.value!)
          }
          if User.sharedInstance.token == "" {
              User.sharedInstance.token =  response.response?.allHeaderFields["Authorization"] as! String
          } else {
              if let header = response.response?.allHeaderFields["Authorization"] as? String {
                  if User.sharedInstance.token != header {
                      User.sharedInstance.token = header
                  }
              }
          }
          completionHandler(data, response.result.error as NSError?)
      }
  }
  </code></pre>
  
  <p>My screen request</p>
  
  <pre><code>class func requestMainScreen(handler: @escaping ([ShortRestaurant], [ShortRestaurant], [ShortRestaurant]) -&gt; ()) {
      var dataForBestChoise: [ShortRestaurant] = []
      var dataForTop: [ShortRestaurant] = []
      var dataForNearest: [ShortRestaurant] = []
  
      let group = DispatchGroup()
      group.enter()
  
      APIModel.sharedInstance.requestWithLocation(.post, URL: "restaurants/near", parameters: nil, completionHandler: {(data, error) in
  
          guard let `data` = data else {
              group.leave()
              return
          }
          for JSON in data["restaurants"].arrayValue {
              dataForNearest.append(ShortRestaurant.initFromJSON(JSON)) //here is leak
          }
          group.leave()
      })
  
      group.enter()
      APIModel.sharedInstance.requestWithLocation(.post, URL: "restaurants/top", parameters: nil, completionHandler: {(data, error) in
          guard let `data` = data else {
              group.leave()
              return
          }
          for JSON in data["restaurants"].arrayValue {
              dataForTop.append(ShortRestaurant.initFromJSON(JSON))//here is leak
          }
          group.leave()
      })
  
      group.enter()
      APIModel.sharedInstance.requestWithLocation(.post, URL: "restaurants/personal", parameters: nil, completionHandler: {(data, error) in
  
          guard let `data` = data else {
              group.leave()
              return
          }
  
          for JSON in data["restaurants"].arrayValue {
               dataForBestChoise.append(ShortRestaurant.initFromJSON(JSON)) //here is leak
          }
          group.leave()
      })
  
      group.notify(queue: DispatchQueue.main) {
          handler(dataForBestChoise, dataForTop, dataForNearest)
      }
  }
  </code></pre>
  
  <p>My classes (I know this kind of initialisation is kinda wrong, but I'd changed to <code>init(data: JSON)</code> - did not help:</p>
  
  <pre><code>class func initFromJSON(_ data: JSON) -&gt; ShortRestaurant {
      let restaurant = ShortRestaurant()
      restaurant.id                       = data["id"].stringValue
      restaurant.name                     = data["name"].stringValue
      restaurant.image                    = data["img"].stringValue
      restaurant.description              = data["shortDesc"].stringValue
      restaurant.nameOfMetrostatin        = data["address"]["metro"]["name"].stringValue
      restaurant.mapType                  = data["mapType"].stringValue
      restaurant.address                  = data["address"]["street"].stringValue
      restaurant.longitude                = data["address"]["location"][0].doubleValue
      restaurant.latitude                 = data["address"]["location"][1].doubleValue
      restaurant.phone                    = data["phone"].stringValue
      restaurant.workTime                 = data["currentWork"].stringValue
      restaurant.avarageBill              = data["price"].stringValue
      restaurant.peopleInfo               = data["croud"].stringValue
      restaurant.rating                   = data["rating"].stringValue
      restaurant.ratingTrend              = data["trend"].stringValue
      restaurant.distance                 = data["distance"].doubleValue
      restaurant.isFavourited             = data["isFavourited"].bool ?? false
      restaurant.specialOfferDescription  = data["discounts"]["name"].string
      restaurant.specialOfferName         = data["discounts"]["type"].string
      restaurant.alertText                = data["label"]["name"].string
      restaurant.alertIcon                = data["label"]["type"].string
      restaurant.alertBackground          = data["label"]["color"].string
      restaurant.avaliableDates           = ReservationSchedule.initArrayFrom(data: data["availableDates"])
      restaurant.avaliableTimes           = data["scheduleRes"].arrayObject as? [String] ?? []
      restaurant.doesHaveDiscount         = data["discounts"]["id"].string != nil
      restaurant.doesHaveEvent            = data["events"]["id"].string != nil
      restaurant.weeklyTop                = data["weeklyTop"].bool ?? false
      restaurant.monthlyTop               = data["monthlyTop"].bool ?? false
      restaurant.yearTop                  = data["yearTop"].bool ?? false
      restaurant.isActive                 = data["isActive"].bool ?? true
      return restaurant
  }
  </code></pre>
  
  <p>Array of these leaks:</p>
  
  <pre><code>class ReservationSchedule {
    var description: String
    var data: String
    var dayTitle: String
    var fullTitle: String
  
    init(data: JSON) {
      self.data = data["value"].stringValue
      self.dayTitle = data["day"].stringValue
      self.description = data["label"].stringValue
      self.fullTitle = data["title"].stringValue
    }
  
    class func initArrayFrom(data: JSON) -&gt; [ReservationSchedule] {
      var schedule: [ReservationSchedule] = []
      for day in data.arrayValue {
          schedule.append(ReservationSchedule.init(data: day)) //here is leak
      }
      return schedule
    }
  }
  </code></pre>