1

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:

Screenshot from Instrument

rmaddy
  • 314,917
  • 42
  • 532
  • 579
Sujesh Surya
  • 143
  • 1
  • 1
  • 12
  • Click on the red diamond and you'll see what exactly the leak is (if there is one). Sometimes a leak is a false positive (eg. right at the launch of your app). – l'L'l Nov 23 '19 at 05:03
  • The information in the lower portion of the screenshot, is what i get after clicking the red diamond. I just expanded all the levels in the error tree, just to show information i see in instrument. – Sujesh Surya Nov 23 '19 at 05:08
  • If something is leaked it's in your `CFNetwork` call. Do you actually see the memory gradually creeping upwards even if the application is completely idle? – l'L'l Nov 23 '19 at 05:13
  • refer this link: https://stackoverflow.com/questions/28223345/memory-leak-when-using-nsurlsession-downloadtaskwithurl/35757989#35757989 – SGDev Nov 23 '19 at 05:22
  • I am calling finishTasksAndInvalidate to invalidate the session, i also have week reference [weak self] in the closure.... Not sure what i am missing... – Sujesh Surya Nov 23 '19 at 05:44
  • You need to drill down into `Allocations > Statistics > Allocation Summary` to find out what the `RefCt` is for whatever object might be leaking. – l'L'l Nov 23 '19 at 06:02
  • HI I'L'I, Not clear, what you mean by this. can u please explain (if possible). – Sujesh Surya Nov 23 '19 at 14:10
  • @I’L’I - the memory usage is static/same if my app is idle – Sujesh Surya Nov 26 '19 at 01:42

1 Answers1

1

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

Ammar
  • 380
  • 6
  • 16