开发者

Memory Leak while creating and copying Images with NSFiIlemanager on iPad App

开发者 https://www.devze.com 2023-03-12 03:58 出处:网络
I have a memory leak which crashes my App while copying / creating images width NSFileManager. When i profile my App with \"Allocations\", everything looks fine. The Allocated Memory Goes up from apr

I have a memory leak which crashes my App while copying / creating images width NSFileManager.

When i profile my App with "Allocations", everything looks fine. The Allocated Memory Goes up from aprox 1.5 MB to 6 MB during every recoursion and then drops to开发者_StackOverflow 1.5MB again.

But the "Real Memory" and "Virtuel Memory" grows to aprox 150MB and then the App crashes.

I receive Memory Warnings Level 1 and 2 before.

here is the function us use:

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

}


Thank you for your Hint.

I fond out, that the Problem was not a leak, but the memory Allocation was too big when scaling down Big Images in "resizedImageWithContentMode:" That made the App crash.

I changed the Image scaling to use the Image I/O framework. Now it works fine.


UPDATE: This answer is outdated. If you are using ARC, ignore it .

How do you allocate NSFileManager?

I have experienced that allocating it via the + defaultManager method, which is deprecated, produces memory leaks (or Instruments says so, Instruments sometimes reports memory leaks where there are not).

Generally, you should allocate it with [[NSFileManager alloc] init] and release when you no longer need it.

0

精彩评论

暂无评论...
验证码 换一张
取 消