Gradual memory leak and slowdown in loop

Posted by Benji XVI on Stack Overflow See other posts from Stack Overflow or by Benji XVI
Published on 2010-06-17T15:29:39Z Indexed on 2010/06/17 15:33 UTC
Read the original article Hit count: 222

Filed under:
|
|
|

I have a simple foundation tool that exports every frame of a movie as a .tiff file. Here is the relevant code:

NSString* movieLoc = [NSString stringWithCString:argv[1]];
QTMovie *sourceMovie = [QTMovie movieWithFile:movieLoc error:nil];
int i=0;

while (QTTimeCompare([sourceMovie currentTime], [sourceMovie duration]) != NSOrderedSame) {
    // save image of movie to disk  
    NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];

    NSString *filePath = [NSString stringWithFormat:@"/somelocation_%d.tiff", i++];
    NSData *currentImageData = [[sourceMovie currentFrameImage] TIFFRepresentation];
    [currentImageData writeToFile:filePath atomically:NO];
    NSLog(@"%@", filePath);

    [sourceMovie stepForward];
    [arp release];
}

[pool drain];
return 0;

As you can see, in order to prevent very large memory buildups with the various transparently-autoreleased variables in the loop, we create, and flush, an autoreleasepool with every run through the loop.

However, over the course of stepping through a movie, the amount of memory used by the program still gradually increases, and the speed at which frames are processed drops precipitously. (From ~0.5 seconds per frame at the start, to ~2 seconds per frame by the 250th frame.)

The only thing I can think can be causing the gradual memory leak is a buildup of the NSAutoreleasePool objects themselves. Am I right in thinking they will only be deallocated when the outer pool is released? If so, is there a better memory management solution here? Creating a pool every run through the loop seems a little hacky. And if not, what is causing the slow memory leak? (It is not NSStrings, and much too slow to be NSImages or NSDatas.)

And what could be causing the slowdown?

© Stack Overflow or respective owner

Related posts about c

    Related posts about objective-c