NSURLSession Memory Leaks occur when using web services in IOS

前端 未结 4 915
面向向阳花
面向向阳花 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:15

    After rereading the URL Loading System Programming Guide it turns that I was setting the NSURLSession property to nil too early.

    Instead, I need to set the NSURLSession property to nil AFTER I receive the delegate message URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error, which makes sense. Luckily, it was a minor mistake.

    E.g.

    - (void)URLSession:(NSURLSession *)session didBecomeInvalidWithError:(NSError *)error
    {
        if (error)
        {
    
    #ifdef DEBUG
            NSLog(@"[GPPhotoRequest] Session Invalidation: %@", [error description]);
    #endif
    
        }
    
        if ([session isEqual:_session])
        {
            [self cleanupSession];
        }
    }
    
    0 讨论(0)
  • 2021-01-31 19:24

    Please see my answer here: https://stackoverflow.com/a/53428913/4437636

    I believe this leak is the same one I was seeing, and only happens when running network traffic through a proxy. My code was fine, but it turned out that an internal bug in the Apple API was causing the leak.

    0 讨论(0)
  • 2021-01-31 19:25

    Had the same issue. The @Jonathan's answer didn't make a sense - my app still leaked memory. I found out that setting the session property to nil in URLSession:didBecomeInvalidWithError: delegate method is causing the issue. Tried to look deeper into the URL Loading System Programming Guide. It says

    After invalidating the session, when all outstanding tasks have been canceled or have finished, the session sends the delegate a URLSession:didBecomeInvalidWithError: message. When that delegate method returns, the session disposes of its strong reference to the delegate.

    I left the delegate method blank. But the invalidated session property still have a pointer, when should I set it nil? I just set this property weak

    // .h-file
    @interface MyClass : NSObject <NSURLSessionDelegate>
    {
      __weak NSURLSession *_session;
    } 
    
    // .m-file
    - (NSURLSessionTask *)taskForRequest:(NSURLRequest *)request  withCompletionHandler:(void(^)(NSData *,NSURLResponse *,NSError *))handler
    {
      if(!_session)
        [self initSession];
      //...
    }
    

    The app stopped leaking memory.

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题