【问题标题】:Memory Issues With ARCARC 的内存问题
【发布时间】:2013-10-08 17:40:10
【问题描述】:

我已通过 Edit-Refactor->Convert to 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];
            }


        }
    }
}

}

内存随着时间的推移不断增加,下载对象并在收到内存警告后崩溃

感谢您提前提供帮助。

【问题讨论】:

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


    【解决方案1】:

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

    代码没有显示内部循环循环了多少次,但是如果将其包装在 @autorelease 块中,它将更频繁地释放分配的内存。

    Read this for more info

    Another SO post on autoreleasepools

    【讨论】:

    • 感谢您的回复...我之前尝试过,但没有任何改变
    【解决方案2】:

    经过 3 天的努力并尝试了您能想象到的一切,好吧: 我做了一个新项目并复制了相同的代码并猜猜是什么 它没有内存问题

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-27
      • 1970-01-01
      • 2012-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-15
      • 2016-02-23
      相关资源
      最近更新 更多