Search code examples
iosobjective-cautomatic-ref-countingnsdatansoperationqueue

Memory Issues With ARC


i have changed a non-ARC project to ARC through Edit-Refactor->Convert to ARC all the leaks had gone now except a function called this way :

NSOperationQueue *queue = [NSOperationQueue new];
    NSInvocationOperation *operation = [[NSInvocationOperation alloc]
                                        initWithTarget:self
                                        selector:@selector(DownloadAll)
                                        object:nil];
    operation.queuePriority=NSOperationQueuePriorityVeryHigh;
    [queue addOperation:operation];

and here is the function:

-(void)DownloadAll{
NSArray *paths = NSSearchPathForDirectoriesInDomains (NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsPath;
bool fileExists;
NSString * strSelect = [NSString stringWithFormat:@"SELECT NoOfAyas from tbSura"];
NSMutableArray * marrItems = [gObj.sqlObj selStatement:strSelect :1];
for (int i =1; i<=114; i++) {
    documentsPath = [NSString stringWithFormat:@"%@/%@/%i",[paths objectAtIndex:0],DefaultReader,i];
    fileExists = [[NSFileManager defaultManager] fileExistsAtPath:documentsPath];
    if (!fileExists)
        [[NSFileManager defaultManager] createDirectoryAtPath:documentsPath withIntermediateDirectories:YES attributes:nil error:nil];

    for (int j =1; j<=[[marrItems objectAtIndex:i-1] intValue]; j++) {
        int indx=[ReadersIDs indexOfObject:DefaultReader];
        NSString *str=[NSString stringWithFormat:@"%@%i/%i.mp3",[ReadersPaths objectAtIndex:indx],i,j];

        documentsPath = [NSString stringWithFormat:@"%@/%@/%i/%@",[paths objectAtIndex:0],DefaultReader,i,[str lastPathComponent]];

        fileExists = [[NSFileManager defaultManager] fileExistsAtPath:documentsPath];
        if (!fileExists) {
            @autoreleasepool {
                NSError *err;
                NSData *data=[NSData dataWithContentsOfURL:[NSURL URLWithString:str] options:0 error:nil];

                if (err) {
                    NSLog(@"%@",err);
                }

                if(!data)
                    NSLog(@"Aya : %i  %i",i,j);
                else
                    [data writeToFile:documentsPath atomically:YES];
            }


        }
    }
}

}

the memory goes up and up with time and downloaded objects and crashes after receiving memory warnings

Thanks for helping in advance.


Solution

  • Ok after working for 3 days on it and trying everything u can imagine: i made a new project and copied the same code and guess what it worked with no memory issue

    i think converting to a non-ARC project to ARC is not accurate