Question

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.

Was it helpful?

Solution 2

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

OTHER TIPS

I think it would make sense to wrap the inner for-loop in a autoreleasepool.

The code doesn't reveal how many time the inner loop is looped over, but if you wrap it in a @autorelease block it will release allocated memory more frequently.

Read this for more info

Another SO post on autoreleasepools

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top