iphone - 帮助我了解此代码 : 的泄漏

标签 iphone ios ios4 uiimage delegation

<分区>

Possible Duplicate:
How to release an object declared into a method and passed to another method?

你能帮我修复这段代码中的漏洞吗:

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
    UIImage *picture = [[UIImage alloc] init];
    if (payload.size.width != kAppIconHeight && payload.size.height != kAppIconHeight)
    {
        CGSize itemSize = CGSizeMake(kAppIconHeight, kAppIconHeight);
        UIGraphicsBeginImageContext(itemSize);
        CGRect imageRect = CGRectMake(0.0, 0.0, itemSize.width, itemSize.height);
        [payload drawInRect:imageRect];
        picture = UIGraphicsGetImageFromCurrentImageContext();
        UIGraphicsEndImageContext();
    }
    else
    {
        picture = payload;
    }

    self.activeDownload = nil;
    [payload release];

    self.imageConnection = nil;

    [delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];
}

感谢您的帮助,

史蒂芬

最佳答案

当您在 if 语句中设置 picture = UIGraphicsGetImageFromCurrentImageContext() 或在 else 中设置 picture = payload 时声明中,您丢失了指向先前分配的 UIImage 的指针,您在第一行的 picture 中分配了它,但您从未释放它。

你不应该为 picture alloc+init 一个新的 UIImage,因为你从来没有使用它并在以后为这个变量分配一个新值......但从来没有使用和释放以前分配的那个。

关于iphone - 帮助我了解此代码 : 的泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7433002/

上一篇:iphone - 我们需要释放常量吗?

下一篇:iphone - 非越狱 iOS 键盘记录器

相关文章:

iphone - 可以安全使用正则表达式吗? (HTML)

ios - UILocalNotification 的 alertTitle 属性不起作用

iphone - NSOperationQueue 并发操作与新线程开始内部操作

ios - Xcode——默认添加新文件到所有目标

ios - 长按 swift 后调用的 didselectrowatindexpath

iphone - 导出 iPhone 地址簿数据库的可能方法

iphone - Objective-c:如何使用标签对 UIButtons 的 NSMutableArray 进行排序

iOS:为什么我要先在 viewDidUnload 中解除分配,然后在 dealloc 方法中解除分配?

iphone - NSURLRequest 的私有(private) "setAllowsAnyHTTPSCertificate:forHost:"的替代方法?

iOS5 : how to determine if the Notification Center for the app is turned ON/OFF