本文介绍了AFNetworking代码给我带来内存泄漏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

@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!"];
        }];
    }

我在上面的这段代码中收到内存泄漏.如您所见,我评论了与泄漏仪器相同的%数量.我正在运行Xcode 6,并且测试是在iPhone设备5s IOS 7.1.1上进行的

I am receiving memory leaks on this code above. As you can see i commented the amount of % same as the Leaks instrument did. Im running Xcode 6, and the test is made on my iPhone device 5s IOS 7.1.1

这是如何泄漏工具的屏幕快照. https://www.dropbox.com/s/beh4no79wgk54bm/Screen%20Shot%202015-03-12%20at%2013.09.53.png?dl=0

Here is a screenshot of how to leaks tool was looking like.https://www.dropbox.com/s/beh4no79wgk54bm/Screen%20Shot%202015-03-12%20at%2013.09.53.png?dl=0

推荐答案

每次调用"postEventInfo"时,您正在创建一个AFHTTPSessionManager对象.

Every time you call "postEventInfo", you're creating a AFHTTPSessionManager object.

如果您使用的是ARC,则应该表示已释放了旧对象(即不是这样的问题).但是,出于最佳实践的考虑,您应该执行以下操作:

If you're using ARC, this should mean the old object gets released (i.e. not such a problem). But for best practices sake you should do something like this:

// 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%
}

这篇关于AFNetworking代码给我带来内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 13:21