I’m developing an iPhone app which displays some tiles from a map. I’ve got a thread which loads tile’s from internet or filesystem. This is an endless thread
while( true ){ //get tile into cache }
Unfortunateley after displaying much tiles the application chrashes with signal 0, which means that im out of memory..
My idea is that in this endless thread the tiles are loaded as autorelease, and are nog autoreleased..
Basically im doing this in my endless thread:
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
while( true ){
UIImage * img = nil;
NSFileHandle * tile = [NSFileHandle fileHandleForReadingAtPath:filePath];
if( tile ){
NSData * data = [tile readDataToEndOfFile];
if( data ){
img = [[UIImage alloc] initWithData:data];
local = YES;
}
}
if( img == nil ){
NSURL * url = [NSURL URLWithString: [NSString stringWithFormat:@"http://tile.openstreetmap.org/%@.png", todoKey ] ];
img = [[UIImage alloc] initWithData: [NSData dataWithContentsOfURL: url options:NSDataReadingMapped error:&error] ];
}
if( img != nil ){
[cache addObject:img]; //cache is an array of tiles
}
[img release];
[self cleanupCache]; //method to check if cache is full, and if so remove's objects from it
}
[pool release];
Im thinking that the autoreleasepool doesn’t clean up the “dead” references from time to time, and all the tiles are kept in memory. When i check with instruments-leaks there aren’t any memory leaks, but the ‘live-memory’ keeps adding up..
Any idea’s why this is happening and how i can prevent it?
You should swap the first 2 lines of the program:
should be
Currently, after the first trip through your loop, the NSAutoreleasePool you have created will be released. After this, all autoreleased objects will be added to a different NSAutoreleasePool higher in the call stack. I suspect this parent autorelease pool is never released while your program is running.