【问题标题】:Help me understand leaks on this code: [duplicate]帮助我了解此代码的泄漏:[重复]
【发布时间】:2011-11-17 23:31:58
【问题描述】:

可能重复:
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]];
}

感谢您的帮助,

斯蒂芬

【问题讨论】:

标签: iphone ios ios4 uiimage delegation


【解决方案1】:

当您在if 语句中设置picture = UIGraphicsGetImageFromCurrentImageContext() 或在else 语句中设置picture = payload 时,您将丢失指向您在第一行中在picture 中分配的先前分配的UIImage 的指针,但您永远不会放出来了。

你不应该为picture分配+初始化一个新的UIImage,因为你以后永远不会使用它并为这个变量分配一个新值......但永远不会使用和释放之前分配的那个。

【讨论】:

  • 但在执行“picture = UIGraphicsGetImageFromCurrentImageContext();”时没有将它保存到我在第 2 行声明的图片实例中 (UIImage *picture = [[UIImage alloc] init];) ?
  • 当然不是。这就像你在做int x = 5 和稍后的一些行x = 12。当然,您的价值 5 将永远丢失。在这里,您为您的 picture 变量分配一个已存在于内存中的对象。您之前使用alloc+init 分配并之前存储在picture 中的对象显然是完全不同的(并且无用且泄露)的对象。
【解决方案2】:
  1. UIImage *picture = [[UIImage alloc] init];picture = UIGraphicsGetImageFromCurrentImageContext(); 您不应该在代码开头初始化pictureUIGraphicsGetImageFromCurrentImageContext 返回一个已经初始化的UIImage(自动释放)。

  2. UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];picture = payload;[payload release];。 您应该改用[payload autorelease],否则您将发布图像而不使用它。

  3. [delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]]; 您应该删除 [picture autorelease] 并使用 picture

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
        UIImage *picture;
        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;
        }
        [payload autorelease];
        self.activeDownload = nil;
    
        self.imageConnection = nil;
    
        [delegate ThumbDidLoad:self.indexPathInTableView Image:picture];
    }
    

【讨论】:

    猜你喜欢
    • 2011-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-11-12
    • 1970-01-01
    • 2021-02-06
    • 2011-07-28
    相关资源
    最近更新 更多