I'm profiling (Leaks) my app (ARC) and it's showing several memory leaks which I can't figure out.
One object (ArchivingTasksManager) kicks off a method that creates many NSOperations within a for in loop, and adds them to a queue. Each NSOperation is a custom subclass (DownloadImageOperation) that I instantiate with an NSURL.
In ArchivingTasksManager:
- (void)archiveAllImages
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
[self beginBackgroundTask];
[self archiveImages];
});
}
- (void)archiveImages
{
self.queue.suspended = true;
self.queue.maxConcurrentOperationCount = 1;
for (Highlight *highlight in self.list.highlights) {
for (Experience *experience in highlight.experiences) {
for (NSURL *imageURL in [experience allImageURLsOfType:ExperienceImageType640x640]) {
DownloadImageOperation *experienceImageDownload = [[DownloadImageOperation alloc] initWithImageURL:imageURL];
[self.queue addOperation:experienceImageDownload];
}
NSURL *authorURL = [NSURL URLWithString:experience.author.image_250x250Url];
if (authorURL) {
DownloadImageOperation *authorImageDownload = [[DownloadImageOperation alloc] initWithImageURL:authorURL];
[self.queue addOperation:authorImageDownload];
}
}
MapSnapshotOperation *mapSnapshot = [[MapSnapshotOperation alloc] initWithHighlight:highlight];
[self.queue addOperation:mapSnapshot];
}
[self.queue addObserver:self
forKeyPath:NSStringFromSelector(@selector(operationCount))
options:NSKeyValueObservingOptionInitial context:nil];
self.totalOperations = self.queue.operationCount;
self.queue.suspended = false;
}
In DownloadImageOperation:
- (instancetype)initWithImageURL:(NSURL *)imageURL;
{
self = [super init];
if (!self) {
return nil;
}
_imageURL = imageURL;
_targetURL = [[ArchivingOperation class] localImageURLFromRemoteImagePath:imageURL.absoluteString];
NSURLSessionConfiguration *config = [NSURLSessionConfiguration ephemeralSessionConfiguration];
_session = [NSURLSession sessionWithConfiguration:config delegate:self delegateQueue:nil];
_downloadTask = [_session downloadTaskWithURL:imageURL];
return self;
}
Here's what Instruments shows me in ArchivingTasksManager:
And here's what Instruments shows me in DownloadImageOperation: