SwiftyJSON: Memory leak in XCode 7.1 beta

I use Alamofire to make an API call and use SwiftyJSON to wrap around the json response. XCode Instrument shows that there’s memory leak created by Alamofire library. Here’s the code (pretty simple) :

Alamofire.request(service.method, service.url, parameters: nil, encoding: ParameterEncoding.URL,
headers: headers).responseJSON(completionHandler: { (req, res, result) -> Void in

       // This line leak the memory
       let json = JSON(result.value!)
    })

If i comment out the line let json = JSON(result.value!) then no more memory leak.

About this issue

  • Original URL
  • State: closed
  • Created 9 years ago
  • Comments: 16 (1 by maintainers)

Commits related to this issue

Most upvoted comments

There is only one block of code that has the switch… as expressions. I modified the following code in the SwiftyJSON.swift file and the memory leak went away:

if let number = newValue as? NSNumber { if number.isBool { _type = .Bool } else { _type = .Number } self.rawNumber = number } else if let string = newValue as? String { _type = .String self.rawString = string } else if let _ = newValue as? NSNull { _type = .Null } else if let array = newValue as? [AnyObject] { _type = .Array self.rawArray = array } else if let dictionary = newValue as? [String : AnyObject] { _type = .Dictionary self.rawDictionary = dictionary } else { _type = .Unknown _error = NSError(domain: ErrorDomain, code: ErrorUnsupportedType, userInfo: [NSLocalizedDescriptionKey: “It is a unsupported type”]) }

                    /*
        switch newValue {
        case let number as NSNumber:
            if number.isBool {
                _type = .Bool
            } else {
                _type = .Number
            }
            self.rawNumber = number
        case  let string as String:
            _type = .String
            self.rawString = string
        case  _ as NSNull:
            _type = .Null
        case let array as [AnyObject]:
            _type = .Array
            self.rawArray = array
        case let dictionary as [String : AnyObject]:
            _type = .Dictionary
            self.rawDictionary = dictionary
        default:
            _type = .Unknown
            _error = NSError(domain: ErrorDomain, code: ErrorUnsupportedType, userInfo: [NSLocalizedDescriptionKey: "It is a unsupported type"])
        }
        */

So this is the right workaround. screenshot 2015-09-18 19 28 58