views:

438

answers:

1

Once again I'm hunting memory leaks and other crazy mistakes in my code. :)

I have a cache with frequently used files (images, data records etc. with a TTL of about one week and a size limited cache (100MB)). There are sometimes more then 15000 files in a directory. On application exit the cache writes an control file with the current cache size along with other useful information. If the applications crashes for some reason (sh.. happens sometimes) I have in such case to calculate the size of all files on application start to make sure I know the cache size. My app crashes at this point because of low memory and I have no clue why.

Memory leak detector does not show any leaks at all. I do not see any too. What's wrong with the code below? Is there any other fast way to calculate the total size of all files within a directory on iPhone? Maybe without to enumerate the whole contents of the directory? The code is executed on the main thread.

NSUInteger result = 0;
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSDirectoryEnumerator *dirEnum = [[[NSFileManager defaultManager] enumeratorAtPath:path] retain];
int i = 0;
while ([dirEnum nextObject]) {
   NSDictionary *attributes = [dirEnum fileAttributes];
   NSNumber* fileSize = [attributes objectForKey:NSFileSize];
   result += [fileSize unsignedIntValue];

   if (++i % 500 == 0) { // I tried lower values too   
      [pool drain];
   }
}
[dirEnum release];
dirEnum = nil;
[pool release];
pool = nil;

Thanks, MacTouch

+4  A: 

Draining the pool "releases" it, it doesn't just empty it. Think of autorelease pools as stacks, so you have popped yours, meaning that all these new objects are going into the main autorelease pool and not being cleaned up until it gets popped. Instead, move the creation of your autorelease pool to inside the loop. You can do something like

NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
int i = 0;
while( shouldloop ) {
  // do stuff
  if( ++i%500 == 0 ) {
    [pool drain];
    pool = [[NSAutoreleasePool alloc] init];
  }
}
[pool drain];
Jason Coco
Thanks. Sh.. I really did not know that. I always was thinking drain just gives the momory free for all autoreleased objects.It's a clasical case of RTFM. :-)Thanks. Now I have to make sure I do it at all places where I use Autoreleasepool the same way. Uff... there are many. :)
MacTouch
MacTouch: read the docs for NSAutoReleasePool -- there are important differences in behaviour between Cocoa and Cocoa-touch.
Olie