Reputation: 2310
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, we create and destroy an autoreleasepool with every run through the loop, which should dispose of the various autoreleased objects created each run.
However, over the course of stepping through a movie, memory usage gradually increases. Instruments is not detecting any memory leaks per se, but the object trace shows certain General Data blocks to be increasing in size.
[Edited out reference to slowdown as it doesn't seem to be as much of a problem as I thought.]
Edit: let's knock out some parts of the code inside the loop & see what we find out...
Test 1
while (banana) {
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
NSString *filePath = [NSString stringWithFormat:@"/somelocation_%d.tiff", i++];
NSLog(@"%@", filePath);
[sourceMovie stepForward];
[arp release];
}
Here we simply loop over the whole movie, creating the filename and logging it.
Memory usage: stable 15MB for the duration.
Test 2
while (banana) {
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
NSImage *image = [sourceMovie currentFrameImage];
[sourceMovie stepForward];
[arp release];
}
Here we add back in the creation of the NSImage from the current frame.
Memory usage: gradually increases. RSIZE is at 60MB by frame 200; 75MB by f300.
Test 3
while (banana) {
NSAutoreleasePool *arp = [[NSAutoreleasePool alloc] init];
NSImage *image = [sourceMovie currentFrameImage];
NSData *imageData = [image TIFFRepresentation];
[sourceMovie stepForward];
[arp release];
}
We've added back in the creation of an NSData object from the NSImage.
Memory usage: gradually increases. 62MB at f200; 75MB at f300. In other words, largely identical.
My guess is it’s a slight memory leak in the underlying system QTMovie uses when currentFrameImage
is invoked.
Upvotes: 1
Views: 667
Reputation: 11
Had the same issue. Try a local autorelease pool within the loop. This helped in my case. See here: http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmAutoreleasePools.html
Upvotes: 1
Reputation: 30228
The easiest way to find a memory leak is to run your program under a profiler. Xcode includes excellent profiling tools, the one you need is under Run->Start with Performance Tool->Leaks. It is pretty simple to use, too.
Upvotes: 1