AFNetworking 代码给我内存泄漏
AFNetworking code giving me Memory Leaks
@implementation GetData
static NSString *string = @"https://afternoon-springs-7986.herokuapp.com/";
static NSString *baseStr = @"https://afternoon-springs-7986.herokuapp.com/updateInformation";
-(void) postEventInfo: (NSDictionary *) eventInfoObject
{
NSURL *url = [NSURL URLWithString:string]; // 6.5%
// NSURL *baseURL = [NSURL URLWithString:@"http://localhost:5000/"];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; // 71%
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 9.7%
[self.manager POST:@"/addEvent/" parameters:eventInfoObject success:^(NSURLSessionDataTask *task, id responseObject) { // 12.9%
[FVCustomAlertView showDefaultDoneAlertOnView:topView withTitle:@"Klart!"];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[FVCustomAlertView showDefaultErrorAlertOnView:topView withTitle:@"Ett fel uppstod, försök igen!"];
}];
}
我收到上面这段代码的内存泄漏。如您所见,我评论了 % 的数量,与 Leaks 工具所做的相同。我是运行Xcode6,测试是在我的iPhone设备5sIOS7.1.1
这是泄漏工具的截图。
https://www.dropbox.com/s/beh4no79wgk54bm/Screen%20Shot%202015-03-12%20at%2013.09.53.png?dl=0
每次调用“postEventInfo
”时,您都在创建一个 AFHTTPSessionManager
对象。
如果您使用的是 ARC,这应该意味着旧对象已被释放(即不是这样的问题)。但为了最佳实践,你应该这样做:
// set self.manager only if it hasn't been created yet
if(!self.manager)
{
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; // 71%
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 9.7%
}
@implementation GetData
static NSString *string = @"https://afternoon-springs-7986.herokuapp.com/";
static NSString *baseStr = @"https://afternoon-springs-7986.herokuapp.com/updateInformation";
-(void) postEventInfo: (NSDictionary *) eventInfoObject
{
NSURL *url = [NSURL URLWithString:string]; // 6.5%
// NSURL *baseURL = [NSURL URLWithString:@"http://localhost:5000/"];
UIWindow *window = [[UIApplication sharedApplication] keyWindow];
UIView *topView = window.rootViewController.view;
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; // 71%
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 9.7%
[self.manager POST:@"/addEvent/" parameters:eventInfoObject success:^(NSURLSessionDataTask *task, id responseObject) { // 12.9%
[FVCustomAlertView showDefaultDoneAlertOnView:topView withTitle:@"Klart!"];
} failure:^(NSURLSessionDataTask *task, NSError *error) {
[FVCustomAlertView showDefaultErrorAlertOnView:topView withTitle:@"Ett fel uppstod, försök igen!"];
}];
}
我收到上面这段代码的内存泄漏。如您所见,我评论了 % 的数量,与 Leaks 工具所做的相同。我是运行Xcode6,测试是在我的iPhone设备5sIOS7.1.1
这是泄漏工具的截图。 https://www.dropbox.com/s/beh4no79wgk54bm/Screen%20Shot%202015-03-12%20at%2013.09.53.png?dl=0
每次调用“postEventInfo
”时,您都在创建一个 AFHTTPSessionManager
对象。
如果您使用的是 ARC,这应该意味着旧对象已被释放(即不是这样的问题)。但为了最佳实践,你应该这样做:
// set self.manager only if it hasn't been created yet
if(!self.manager)
{
self.manager = [[AFHTTPSessionManager alloc] initWithBaseURL:url]; // 71%
self.manager.requestSerializer = [AFJSONRequestSerializer serializer];
self.manager.responseSerializer = [AFJSONResponseSerializer serializer]; // 9.7%
}