iPhone: Low memory crash...

Posted by MacTouch on Stack Overflow See other posts from Stack Overflow or by MacTouch
Published on 2010-05-08T15:12:39Z Indexed on 2010/05/08 15:18 UTC
Read the original article Hit count: 228

Filed under:
|
|

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

© Stack Overflow or respective owner

Related posts about iphone

Related posts about memory-leaks