2011-01-19 33 views
0

我知道这一定是简单的东西,我俯瞰,但为什么这泄漏:NSAutoreleasePool泄漏

//add a pool for this since it is on its own thread 
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init]; 

//add the image 
NSURL * imageURL = [NSURL URLWithString:[recipeData objectForKey:@"imagePath"]]; 
NSData * imageData = [NSData dataWithContentsOfURL:imageURL]; 
UIImage * image = [UIImage imageWithData:imageData]; 
UIImageView * myImageView = [[UIImageView alloc] initWithImage:image]; 

//resize to make it fit the screen space 
CGRect frame = myImageView.frame; 
frame.size.width = 320; 
frame.size.height = 357; 
myImageView.frame = frame; 

[self.view addSubview:myImageView]; 


[activity stopAnimating]; 

[pool drain]; 
[self placeItems]; 

我得到的错误:

_NSAutoreleaseNoPool():对象0x4e2fdf0类NSPathStore2自动释放与没有到位的地方 - 只是漏水

我试着移动[pool drain]的位置,但是什么也没做。在Google搜索某个原因时,我看到很多代码看起来就像这样。

感谢您的帮助。

+1

您正在泄漏`myImageView`(它需要被释放)。除此之外,乍一看,一切都很好。 – kubi 2011-01-19 15:44:38

回答

2

排空池会释放并随后释放它,因为autoreleasepools不能保留。我怀疑在placeItems(或其他一些地方叫做[pool drain]之后)中必须有一些autoreleasepool需要,因为在那个时候池已经可用了。

因此,您可能需要尝试评论排水消息,看看是否会导致泄漏消失。

+0

修复了它。我只是得到一个警告“未使用的变量池” – 2011-01-19 16:05:59

2

很多事情要在这里说:

  • 第一,你漏myImageView。你必须在-addSubview之后释放它。
  • 接下来,由于您在另一个线程中,因为您不在主线程中,所以您无法执行任何UI操作,因为您的[pool pool]必须位于末尾
  • 。尝试用[self.view performSelectorOnMainThread:@selector(addSubview:) withObject:myImageView waitUntilDone:YES]替换[self.view addSubview:myImageView]。同[activity stopAnimating]

而且像Brian说的那样,-drain消息必须在您的线程结束。