【发布时间】:2011-07-21 22:36:53
【问题描述】:
我正在开发一个 iPhone 应用程序,它显示地图上的一些图块。我有一个从互联网或文件系统加载磁贴的线程。这是一个没完没了的线程
while( true ){ //get tile into cache }
不幸的是,在显示很多图块后,应用程序以信号 0 崩溃,这意味着我的内存不足..
我的想法是,在这个无穷无尽的线程中,瓷砖被加载为自动释放,并且不会自动释放..
基本上我在我无尽的线程中这样做:
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];
我认为自动释放池不会不时清理“死”引用,并且所有图块都保存在内存中。当我检查仪器泄漏时,没有任何内存泄漏,但“实时内存”不断增加..
知道为什么会发生这种情况以及如何防止它吗?
【问题讨论】:
-
为什么需要无限循环?
-
在这个循环中,我正在按需下载切片,如果我的视图问:“你有这些切片吗?”线程负责下载它们并将它们放入缓存中。跨度>