Lefora Free Forum
Loading
854 views

My First Cocoa Program newbie

Page 1
1–5
rookie - member
4 posts



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


?
288 posts


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.


__________________
?
288 posts




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);
    ....
}

__________________
rookie - member
4 posts

Thanks alot. I was going completly mental there. It now uses a nice 19MB ram, and no leaks.

?
288 posts

Always happy to be of help.  Come back and see us again and share you're Cocoa experiences with us all.

__________________
Page 1
1–5

Locked Topic


You must be a member to post in this forum

Join Now!