ARC的内存问题

时间:2013-10-08 17:40:10

标签: ios objective-c automatic-ref-counting nsdata nsoperationqueue

我已通过Edit-Refactor->转换为ARC将非ARC项目更改为ARC 除了以这种方式调用的函数之外,所有泄漏现在已经消失了:

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

这是函数:

-(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];
            }


        }
    }
}

}

内存随着时间的推移而上升,下载对象并在收到内存警告后崩溃

感谢您提前帮助。

2 个答案:

答案 0 :(得分:0)

我认为将内部for循环包装在自动释放池中是有意义的。

代码没有透露内循环被循环的时间,但是如果你将它包装在@autorelease块中,它将更频繁地释放分配的内存。

Read this for more info

Another SO post on autoreleasepools

答案 1 :(得分:0)

在工作了3天之后好了,尝试了你能想象到的一切: 我做了一个新项目并复制了相同的代码并猜测是什么 它没有内存问题

我认为转换为ARC的非ARC项目并不准确