My First Cocoa Program newbie
I need some help understanding what im doing wrong here. The following program consumes all my memory. The point of the program is simple. It reads the DateTimeOriginal Exif tag from all the images in ~/Pictures/. Recursively!
Any help would be welcome thanks.
NSString* getDateTimeOriginal(NSString *path){
[path retain];
NSData *imageData = [[NSData alloc] initWithContentsOfFile:path];
[path release];
CGImageSourceRef imageSource = CGImageSourceCreateWithData((CFDataRef)imageData, NULL);
CFDictionaryRef properties = CGImageSourceCopyPropertiesAtIndex(imageSource, 0, NULL);
[properties autorelease];
[imageData release];
NSString *e = @"{Exif}";
[e autorelease];
if(!CFDictionaryContainsKey(properties, e))
return @"";
CFDictionaryRef exif = CFDictionaryGetValue(properties, e);
[exif autorelease];
NSString *d = @"DateTimeOriginal";
[d autorelease];
if(!CFDictionaryContainsKey(exif, d))
return @"";
NSString *dateTimeOriginal = CFDictionaryGetValue(exif, d);
[dateTimeOriginal autorelease];
return dateTimeOriginal;
}
int main(int argc, char *argv[]){
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSFileManager *fm = [[NSFileManager alloc] init];
NSArray *dc = [fm subpathsOfDirectoryAtPath: @"/Users/abo/Pictures/" error: NULL];
[fm release];
NSPredicate *regex = [NSPredicate predicateWithFormat: @"SELF MATCHES %@ OR SELF MATCHES %@ OR SELF MATCHES %@", @".+\.[nN][eE][fF]$", @".+\.[jJ][pP][eE][gG]$", @".+\.[jJ][pP][gG]$"];
for(NSString *f in dc){
if([regex evaluateWithObject:f]){
NSString *path = [[NSString alloc] initWithFormat: @"%@%@", @"/Users/abo/Pictures/", f];
NSString *result = getDateTimeOriginal(path);
NSLog(@"%@ = %@", path, result);
[result release];
[path release];
[f release];
}
}
[regex release];
[dc release];
return 0;
}
On a quick inspection my guess is that CGImageSourceRef imageSource is eating the machine. And I have a feeling that he creates a bitmap - so you could easily be eating about 20mb on every photograph. Use CFRelease to free him in your loop. The initWithContents of file's another hog - he'll pull the file into memory. Be sure to [imageSource release] to help him escape.
If that doesn't fix it, please send my your code. Please delete the build directory, zip it and email it to robin@clanmills.com and I'll look it for you.
Be sure to [imageSource release] to help him escape.-clanmills
Wrong that should be:
NSString* getDateTimeOriginal(NSString *path){
{
....
CGImageSourceRef imageSource = bla bla
....
[imageData release]; // which I now see you have
...-
CFRelease(imageSource);
....
}
Thanks alot. I was going completly mental there. It now uses a nice 19MB ram, and no leaks.