Objective c 在iPad应用程序上使用NSFiIlemanager创建和复制图像时内存泄漏
我的应用程序在复制/创建图像时发生内存泄漏,导致应用程序崩溃 当我用“分配”配置我的应用程序时,一切看起来都很好。在每次重新分配期间,分配的内存从大约1.5 MB增加到6 MB,然后再次下降到1.5 MB 但“真实内存”和“虚拟内存”增长到约150MB,然后应用程序崩溃 我在之前收到1级和2级内存警告 以下是我们使用的功能:Objective c 在iPad应用程序上使用NSFiIlemanager创建和复制图像时内存泄漏,objective-c,ios,nsfilemanager,Objective C,Ios,Nsfilemanager,我的应用程序在复制/创建图像时发生内存泄漏,导致应用程序崩溃 当我用“分配”配置我的应用程序时,一切看起来都很好。在每次重新分配期间,分配的内存从大约1.5 MB增加到6 MB,然后再次下降到1.5 MB 但“真实内存”和“虚拟内存”增长到约150MB,然后应用程序崩溃 我在之前收到1级和2级内存警告 以下是我们使用的功能: -(void) processCacheItems:(NSMutableArray*) originalFiles { if ( [originalFiles co
-(void) processCacheItems:(NSMutableArray*) originalFiles
{
if ( [originalFiles count] == 0 )
{
[originalFiles release];
return;
}
else
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSString *curFileName = [originalFiles lastObject];
NSString *filePath = [documentsDirectoryPath stringByAppendingPathComponent:curFileName];
NSURL *fileURL = [NSURL fileURLWithPath:filePath];
CGSize destinationSize = CGSizeMake(150,150);
CGSize previewDestinationSize = CGSizeMake(1440.0, 1440.0);
UIImage *originalImage = [UIImage imageWithContentsOfFile:filePath]; // AUTORELEASED
// create thumb and copy to presentationfiles directory
UIImage *thumb = [originalImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit
bounds:destinationSize
interpolationQuality:kCGInterpolationHigh]; // AUTORELEASED
// the resizedImageWithContentMode: does not semm to make the problem, because when i skip this and just use the original file the same problem occours
NSString *thumbPath = [thumbsDirectoryPath stringByAppendingPathComponent:curFileName];
[fileManager createFileAtPath:thumbPath contents:UIImageJPEGRepresentation(thumb, 0.9) attributes:NULL];
// create thumb and copy to presentationfiles directory
UIImage *previewImage = [originalImage resizedImageWithContentMode:UIViewContentModeScaleAspectFit
bounds:previewDestinationSize
interpolationQuality:kCGInterpolationHigh]; // AUTORELEASED
NSString *previewImagePath = [previewsDirectoryPath stringByAppendingPathComponent:curFileName];
[fileManager createFileAtPath:previewImagePath contents:UIImageJPEGRepresentation(previewImage, 0.9) attributes:NULL];
// copy copy original to presentationfiles directory
NSString *originalPath = [originalFilesDirectoryPath stringByAppendingPathComponent:curFileName];
[fileManager copyItemAtPath:filePath toPath:originalPath error:NULL];
[originalFiles removeLastObject];
[pool drain];
[self processCacheItems:originalFiles]; // recursion
}
}
更新:这个答案已经过时了。如果您使用的是ARC,请忽略它 如何分配NSFileManager 我曾经体验过,通过
+defaultManager
方法分配内存会导致内存泄漏(或者仪器会这样说,仪器有时会在没有内存泄漏的地方报告内存泄漏)
通常,您应该使用
[[NSFileManager alloc]init]
分配它,并在不再需要时释放它。谢谢您的提示
我发现问题不是泄漏,而是在“resizedImageWithContentMode”中缩小大图像时内存分配太大,导致应用程序崩溃
我更改了图像缩放以使用图像I/O框架。
现在它工作正常。转到生成设置并启用“运行静态分析器”。使用仪器工具查找内存泄漏的位置。@iAmitWagh我在仪器中看不到内存泄漏。。在“分配”中,它看起来也不错。@WTP。我已启用“运行静态分析器”,现在可以看到什么?在哪里呢?我看不出你的代码中有任何明显的漏洞,但是使用递归并在数组为空时释放数组并不是一种很好的组织方法。为什么不遍历数组,然后释放它呢?
processCacheItems:
的方法名并不表示它将释放您给它的参数。