Calling AFNetworking in NSTimer causes serious memory leak

一笑奈何 提交于 2020-01-06 15:11:31

问题


I'm trying to debug a difficult memory leak. In MyAppDelegate.m, I have

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    self.notificationTimer = [NSTimer scheduledTimerWithTimeInterval:1
                                                              target:self
                                                            selector:@selector(syncNotifications:)
                                                            userInfo:nil
                                                             repeats:YES];
    return YES;
}

- (void)syncNotifications:(NSTimer*)timer {
    NSString *path = @"http://example";
    NSLog(@"GET: %@", path);
    AFHTTPRequestOperationManager *network = [AFHTTPRequestOperationManager manager];
    [network GET:path parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSLog(@"get success");
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@", error);
    }];
}

I see memory leaking at the rate of ~1MB / 3 seconds. I've tried setting the network to a property. Also tried commenting out the actual network request as follows:

- (void)syncNotifications:(NSTimer*)timer {
    NSString *path = @"http://example";
    NSLog(@"GET: %@", path);
    AFHTTPRequestOperationManager *network = [AFHTTPRequestOperationManager manager];
}

And while I don't see as much memory leaked, I do still see memory allocations climbing in the Instruments panel. Am I missing something when it comes to memory management for NSTimers?


回答1:


The problem is that you are creating a new instance of AFHTTPRequestOperationManager every second. The implementation of the manager method looks like this:

+ (instancetype)manager {
    return [[self alloc] initWithBaseURL:nil];
}

So if you create the manager once and hold on to it you should be able to fix your memory leak.




回答2:


After a lot of testing, I found that the "problem" seemed to be with AFHTTPRequestOperation. I didn't see any "leaks" when switching to the AFHTTPSessionManager.

For more information see: https://github.com/AFNetworking/AFNetworking/issues/2596



来源:https://stackoverflow.com/questions/29011278/calling-afnetworking-in-nstimer-causes-serious-memory-leak

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!