【问题标题】:AFNetworking memory leakAFNetworking 内存泄漏
【发布时间】:2019-08-10 23:41:08
【问题描述】:

我使用下面的代码开始下载

AFURLSessionManager *downloadManager1  = [[AFURLSessionManager alloc] initWithSessionConfiguration:NSURLSessionConfiguration.defaultSessionConfiguration];


NSURLSessionDownloadTask *downloadTask;
downloadTask=[downloadManager1 downloadTaskWithRequest:request progress:nil

        destination:^NSURL *(NSURL *targetPath, NSURLResponse *response){

        NSURL *aURL = [[NSFileManager defaultManager] URLForDirectory:NSDocumentDirectory inDomain:NSUserDomainMask appropriateForURL:nil create:NO error:nil];

        return  [aURL URLByAppendingPathComponent:[response suggestedFilename]];

    }completionHandler:^(NSURLResponse *response, NSURL *filePath, NSError *error) {

    }
];
[downloadTask resume];

报内存泄漏,我检查了上面的代码,没发现哪里不对,

欢迎您的评论

【问题讨论】:

    标签: ios objective-c macos cocoa afnetworking


    【解决方案1】:
    invalidateSessionCancelingTasks:
    

    这是已知且记录在案的行为。完成会话后,调用 invalidateSessionCancelingTasks:。对于大多数应用程序来说,这不是问题,它们在应用程序的生命周期内保持一个会话。

    https://github.com/AFNetworking/AFNetworking/issues/1528#issuecomment-26887778

    【讨论】:

      【解决方案2】:

      我认为泄漏发生在您创建经理时。 NSURLSession 持有对委托的保留引用。 如果您只初始化和存储一次管理器可能会有所帮助。我不确定你的 datatask 代码被调用了多少次。

      结帐https://github.com/AFNetworking/AFNetworking/issues/3787

      【讨论】:

        【解决方案3】:

        您有两个指针,一个指向downloadManager1,一个指向NSURL 对象。根据您的代码中调用的位置,我怀疑有一个永久的strong 引用一个或另一个。尝试将 NSURL 从指针转换为实际对象,它会改为按值复制,并检查以确保在工作完成后释放 downloadManager。

        【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-02-28
        • 2016-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多