I ran my app through instruments to see if there were any memory leaks. I have noticed two leaks showing up. One leak only happens at start up which is this:
Line 4 is the offending bit of code here:
static id (*_old_UIStoryboard_instantiateViewControllerWithIdentifier_)(UIStoryboard *self, SEL _cmd, NSString *identifier);
static id _UIStoryboard_instantiateViewControllerWithIdentifier_(UIStoryboard *self, SEL _cmd, NSString *identifier)
{
id ret = _old_UIStoryboard_instantiateViewControllerWithIdentifier_(self, _cmd, identifier);
if ([[self delegate] respondsToSelector:@selector(storyboard:didInstantiateViewController:withIdentifier:)])
[[self delegate] storyboard:self didInstantiateViewController:ret withIdentifier:identifier];
return ret;
}
The biggest leak I have is actually in AFNetworkings code. I am not sure if its something I am doing that causes it though. In the class file: AFURLSessionManager.m
- (instancetype)initWithSessionConfiguration:(NSURLSessionConfiguration *)configuration {
self = [super init];
if (!self) {
return nil;
}
if (!configuration) {
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
}
self.sessionConfiguration = configuration;
self.operationQueue = [[NSOperationQueue alloc] init];
self.operationQueue.maxConcurrentOperationCount = 1;
self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
self.responseSerializer = [AFJSONResponseSerializer serializer];
self.securityPolicy = [AFSecurityPolicy defaultPolicy];
self.reachabilityManager = [AFNetworkReachabilityManager sharedManager];
self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];
self.lock = [[NSLock alloc] init];
self.lock.name = AFURLSessionManagerLockName;
[self.session getTasksWithCompletionHandler:^(NSArray *dataTasks, NSArray *uploadTasks, NSArray *downloadTasks) {
for (NSURLSessionDataTask *task in dataTasks) {
[self addDelegateForDataTask:task completionHandler:nil];
}
for (NSURLSessionUploadTask *uploadTask in uploadTasks) {
[self addDelegateForUploadTask:uploadTask progress:nil completionHandler:nil];
}
for (NSURLSessionDownloadTask *downloadTask in downloadTasks) {
[self addDelegateForDownloadTask:downloadTask progress:nil destination:nil completionHandler:nil];
}
}];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidResume:) name:AFNSURLSessionTaskDidResumeNotification object:nil];
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(taskDidSuspend:) name:AFNSURLSessionTaskDidSuspendNotification object:nil];
return self;
}
These lines from the above method show up as leaking memory - every time this method is called:
if (!configuration) {
configuration = [NSURLSessionConfiguration defaultSessionConfiguration];
}
self.operationQueue = [[NSOperationQueue alloc] init];
The worse one showing the most leaks:
self.session = [NSURLSession sessionWithConfiguration:self.sessionConfiguration delegate:self delegateQueue:self.operationQueue];
And this:
self.mutableTaskDelegatesKeyedByTaskIdentifier = [[NSMutableDictionary alloc] init];
Then the last one:
self.securityPolicy = [AFSecurityPolicy defaultPolicy];
How can I solve these leaks? Also, I would like to know why are they are leaking memory and not just the solution, would like to know more about these cases.