AFHTTPSessionManager无法解除分配

nat*_*203 7 memory-management objective-c afnetworking-2

更新

在将此问题作为问题发布到AFNetworking回购后,事实证明这实际上是我的使用问题.根据我的问题回复:

NSURLSession保留其委托(即AFURLSessionManager).调用invalidateSessionCancelingTasks:确保会话最终确定并释放其委托.

所以,长话短说:如果你AHTTPSessionManager按照下面描述的方式使用,请务必致电invalidateSessionCancelingTasks:以确保会议最终确定并释放他们的代表

原始问题

我有一个子类AFHTTPSessionManager称为GTAPIClient我用来连接到我的REST API.我意识到文档声明要用作单例,但在某些情况下我需要编写一个新实例.但是,似乎每当我这样做时,对象永远不会被释放.目前,在解除分配时,GTAPIClient除了NSLog自身之外,什么都不做.这是一些演示行为的示例代码

GTAPIClient.m

@implementation GTAPIClient
- (void)dealloc
{
    NSLog(@"Dealloc: %@", self);    
}

@end
Run Code Online (Sandbox Code Playgroud)

GTViewController.m

#import "GTBaseEntityViewController.h"

//Models
#import "GTBaseEntity.h"

//Clients
#import "GTAPIClient.h"

@interface GTBaseEntityViewController ()

@property (nonatomic, weak) GTAPIClient *client;
@property (nonatomic, weak) GTBaseEntity *weakEntity;

@end

@implementation GTBaseEntityViewController

- (IBAction)makeClient:(id)sender {

    self.client = [[GTAPIClient alloc] init];
    NSLog(@"I just made an API client %@", self.client);

    //Another random object assigned to a similar property, just to see what happens.
    self.weakEntity = [[GTBaseEntity alloc] init];
    NSLog(@"I just made a random object %@", self.weakEntity);

}

- (IBAction)checkClient:(id)sender {

    NSLog(@"Client: %@", self.client);
    NSLog(@"Entity: %@", self.weakEntity);

}

@end
Run Code Online (Sandbox Code Playgroud)

NSLog输出

makeClient:

//It seems to me that both NSLog's should return (null) as they are assigning to a weak property
2014-06-22 16:41:39.143 I just made an API client <GTAPIClient: 0x10b913680, baseURL: (null), session: <__NSCFURLSession: 0x10b915010>, operationQueue: <NSOperationQueue: 0x10b9148a0>{name = 'NSOperationQueue 0x10b9148a0'}>
2014-06-22 16:41:39.144 I just made a random object (null)
Run Code Online (Sandbox Code Playgroud)

checkClient

//Again, both NSLog's should return null for the two objects. However...client is still around. Also, it's overridden dealloc method never fired.

2014-06-22 16:44:43.722  Client: <GTAPIClient: 0x10b913680, baseURL: (null), session: <__NSCFURLSession: 0x10b915010>, operationQueue: <NSOperationQueue: 0x10b9148a0>{name = 'NSOperationQueue 0x10b9148a0'}>
2014-06-22 16:44:43.723 Entity: (null)
Run Code Online (Sandbox Code Playgroud)

作为参考,我正在使用AFNetworking的v2.3.1.编译器警告我,将保留对象分配给weak属性将在赋值后释放 - 这是正确的,并且随我的随机对象一起运行.应用程序中没有其他任何内容.没有其他视图控制器,没有其他方法GTAPIClient,所有单例功能都被删除.对我在这里做错了什么的想法?

Aar*_*ger 10

发布Mattt Thompson的回复,以协助未来的读者:

NSURLSession 保留其代表(即AFURLSessionManager).致电invalidateSessionCancelingTasks:确保会议最终确定并释放其代表.

如果像许多应用程序一样,您的应用程序使用单个会话管理器和整个应用程序的一个URL会话,那么您无需担心这一点.


Jon*_*ier 2

复制您的场景并通过 Instruments 运行它表明 AFURLSessionManager 由它们创建的 NSURLSession 保留,因为 AFURLSessionManager 充当创建的每个 NSURLSession 的委托。这会创建一个保留周期,因此 AFHTTPSessionManager 无法被释放。我不确定这是否是任一库中的错误或根本不是错误。您可能需要在 AFNetworking GitHub 页面 ( https://github.com/AFNetworking/AFNetworking ) 上报告该问题。