NSURLSession Memory Leaks occur when using web services in IOS

前端 未结 4 920
面向向阳花
面向向阳花 2021-01-31 18:54

I am building an app that uses a web service and to get information from that web service I use NSURLSession and NSURLSessionDataTask.

I am now

4条回答
  •  难免孤独
    2021-01-31 19:34

    I had this same "leaky", memory management issue when I switched to NSURLSession. For me, after creating a session and resuming/starting a dataTask, I was never telling the session to clean itself up (i.e. release the memory allocated to it).

    // Ending my request method with only the following line causes memory leaks
    [dataTask resume];
    
    // Adding this line fixed my memory management issues
    [session finishTasksAndInvalidate];
    

    From the docs:

    After the last task finishes and the session makes the last delegate call, references to the delegate and callback objects are broken.

    Cleaning up my sessions fixed the memory leaks being reported via Instruments.

提交回复
热议问题