Reputation: 163
I need some help in identifying what's causing these memory leak's. I created a simple program to invoke an api and get data. It works as expected. But in instrument i get memory leak's. I have been trying hard to identify whats causing this memory leak, but no luck. I also tried in Debug Memory graph, when i filter "Show Only Leaked Blocks", there are no blocks displayed. Below is the code and screen shot from Instrument:
import UIKit
class ViewController: UIViewController {
struct main_struct {
var ACCEPT_DATE: String
init(_ dictionary: [String: Any]) {
self.ACCEPT_DATE = dictionary["ACCEPT_DATE"] as? String ?? " "
}
}
var main_array = [main_struct]()
override func viewDidLoad() {
super.viewDidLoad()
let Endpoint: String = "http://mylink.com"
let url = URL(string: Endpoint)
downloaddatafromurl(url: url!, completionhandler: { [weak self] (data, response: URLResponse, Error) -> Void in
if Error != nil {
print("error in API Connect")
}
let jsondata = try? JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? [String : NSArray]
let arrayJSON = jsondata?["apidata"]
for dict in arrayJSON! {
self!.main_array.append(main_struct(dict as! [String : Any]))
}
//print(data)
print(response)
})
}
func downloaddatafromurl(url: URL, completionhandler: @escaping (Data, URLResponse , Error? ) -> Void) {
let session = URLSession.shared
let task = session.dataTask(with: url, completionHandler: {data, response, error -> Void in
DispatchQueue.global().async {
completionhandler (data!, response!, error)
}
})
task.resume()
session.finishTasksAndInvalidate()
}
}
Screenshot from Instrument:
Upvotes: 1
Views: 2101
Reputation: 380
You can track memory leaks with Instruments: see this tutorial.
If you're familiar with Valgrind, you use it on x86 binaries built against the iPhone Simulator SDK: see how Landon Fuller does it.
Another Stackoverflow answer suggests the Clang analyser: static analysis of the code may detect memory allocation errors as well. I never used this tool myself but it's good to know it's possible.
Also also Apple's Introduction to Instruments User Guide
Upvotes: 1