Xcode Analyze抱怨说我在标记为"this line"的行上错误地减少了ref count.这看起来有点奇怪,因为这条线减少参考计数并不明显.
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
UIImage * image = [[UIImage alloc] initWithData:receivedData];
if (image == nil) {
image = [UIImage imageNamed:@"null.bmp"];
}
self.itemImage.image = image; //this line
self.promotion.image = image;
[image release];
}
Run Code Online (Sandbox Code Playgroud)
这是一个有点棘手的事情; 这是可以理解的,它引起了混乱.这里的两个路径导致image保持具有不同所有权状态的对象,从而导致不同的引用计数.
通过持有代码不拥有的对象来查看if结果image.
UIImage * image = [[UIImage alloc] initWithData:receivedData];
// If |initWithData:| succeeds, the object in |image| is owned, because you
// called |alloc| to create it.
if (image == nil) {
image = [UIImage imageNamed:@"null.bmp"];
// This object is _not_ owned by your code and you must not send
// |release| to it.
}
// ... the setter lines are irrelevant to the reference count of |image|.
[image release];
// This is only okay if |initWithData:| succeeded and you have ownership
// of |image|.
Run Code Online (Sandbox Code Playgroud)
我认为分析器指示错误的行,就像编译器会抱怨丢失分号后五行一样.
解决这个问题的方法可能是留住你得到的图像imageNamed:你不能只是不发送release,因为你做自己image在一种情况下,你需要正确放弃所有权.我还建议在那里发表关于发送的评论,retain这样你就会记得八个月后你为什么这么做.
更好的是,正如下面由无法模仿的Bavarious所建议的那样,并且正如我自己已经想到的那样,将会自动释放alloc'd图像并删除后release一行.
| 归档时间: |
|
| 查看次数: |
141 次 |
| 最近记录: |