在NSTimer中调用AFNetworking会导致严重的内存泄漏 [英] Calling AFNetworking in NSTimer causes serious memory leak
问题描述
我正在尝试调试内存泄漏难题.在MyAppDelegate.m中,我有
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);
}];
}
我看到内存泄漏的速度约为1MB/3秒.我尝试将网络设置为属性.还尝试注释掉实际的网络请求,如下所示:
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];
}
虽然我看不到有太多的内存泄漏,但是我仍然看到仪器"面板中的内存分配正在增加.在涉及NSTimers的内存管理时,我是否缺少某些东西?
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?
推荐答案
经过大量测试,我发现问题"似乎与AFHTTPRequestOperation有关.切换到AFHTTPSessionManager时没有看到泄漏".
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.
有关更多信息,请参见: https://github.com/AFNetworking/AFNetworking/issues/2596
For more information see: https://github.com/AFNetworking/AFNetworking/issues/2596
这篇关于在NSTimer中调用AFNetworking会导致严重的内存泄漏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!