Iphone 为什么这是内存泄漏?UIImage`cellForRowAtIndexPath:`
仪器泄漏告诉我此UIImage正在泄漏:Iphone 为什么这是内存泄漏?UIImage`cellForRowAtIndexPath:`,iphone,objective-c,memory-management,memory-leaks,uiimage,Iphone,Objective C,Memory Management,Memory Leaks,Uiimage,仪器泄漏告诉我此UIImage正在泄漏: UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png", [postsArrayID objectAtIndex:indexPath.row]]]]; // If image contains anything, set cellIma
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png", [postsArrayID objectAtIndex:indexPath.row]]]];
// If image contains anything, set cellImage to image. If image is empty, try one more time or use noImage.png, set in IB
if (image != nil){
// If image != nil, set cellImage to that image
cell.cellImage.image = image;
}
image = nil;
[image release];
(类单元格(自定义表视图单元格)也在dealoc方法中释放cellImage)
我不知道它为什么漏水,但确实是。
图像在cellforrowatinexpath:
-方法中多次加载。前三个单元的图像没有泄漏(130px高,所有空间可用)
Leaks只提供了在code Leaks中分配的UIImage的信息
你能帮我弄清楚吗?谢谢:)在调用release
之前,您正在将其设置为nil
。因此,您正在释放nil
,而不是image
更改为:
image = nil;
[image release];
致:
如果图像是@属性
,则您在那里的代码是正确的。由于setter的工作方式,您可以通过执行self.property=nil来释放@property。setter释放旧对象并将ivar设置为值。要解决此问题,您需要先放置[图像发布]
。这里发生的事情是,您将image设置为nil,然后实际上您正在执行[nil release]
。旧的图像只是漂浮在某处。因此,要解决此问题,请执行以下操作:
[image release];
image = nil;
微位码修改
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath
stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png",
[postsArrayID objectAtIndex:indexPath.row]]]];
if (image){
cell.cellImage.image = image;
[image release];
哦,我没意识到那会导致泄漏。。!谢谢:)在这种情况下你会用什么?这两者之间有什么区别:[[UIImage alloc]initWithContentsOfFile:…]
和[UIImage imageWithContentsOfFile:…]代码>。有什么区别吗?我真的不知道。imageWithContentsOfFile
是一种方便的方法。因此,它负责发布图像。如果你打电话给alloc,你就要负责。你可以在苹果内存管理指南中看到:该死,50页。。我的打印机不可能接受:(像现在这样的时候,我希望我有一台iPad,然后我就可以真正阅读苹果的文档了。)HIg有136页长,而且一直都在变化,所以我想打印出来也没有意义。我需要一台iPad。。使用带有内容的图像文件和你发送文件是一样的[图像自动释放]。该类方法只返回一个自动释放的对象。在iPhone上最好不要这样做,因为你永远不知道自动释放池将在何时耗尽。通过这种方式,你可以确保在正确的时间修改图像的保留计数。哦,将已释放的对象设置为nil
any有什么意义方法?主要原因是,如果你出于某种原因对其调用一个方法,它将不会作为EXC_BAD_ACCESS返回。如果你向发布的对象发送消息,你的应用程序将崩溃,EXC_BAD_ACCESS作为正确的原因(关于@property在设置新值之前发布)仅当@property的setter语义为retain
。如果您使用了assign
或copy
(即您说的@property(非原子,assign)Type*name
),它不会在新的assign上发布,并且会泄漏。大多数情况下,您将在那里使用retain
,但这将有助于了解您这样做时发生的情况。Dan,您的权利。我忘了提到这一点,感谢您为emil澄清。在这种情况下,您会使用什么?这两者之间的区别是什么:[[UIImage alloc]initWithContentsOfFile:…];和[UIImage imageWithContentsOfFile:…];。有什么区别吗?
UIImage *image = [[UIImage alloc] initWithContentsOfFile:[imagesPath
stringByAppendingPathComponent:[NSString stringWithFormat:@"/%@.png",
[postsArrayID objectAtIndex:indexPath.row]]]];
if (image){
cell.cellImage.image = image;
[image release];