帮助我理解这段代码的泄漏:

发布于 2024-12-04 21:47:34 字数 1170 浏览 2 评论 0原文

可能的重复:
如何释放对象声明到一个方法中并传递给另一个方法?

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

- (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]];
}

谢谢你的帮助,

Stephane

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

Can you help me fix leaks in this code:

- (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]];
}

Thx for helping,

Stephane

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

暖心男生 2024-12-11 21:47:34

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

您不应该为 picture 分配+初始化一个新的 UIImage,因为您从不使用它并稍后为该变量分配新值...但从未使用和释放之前分配的值。

When you set picture = UIGraphicsGetImageFromCurrentImageContext() in your if statement or picture = payload in your else statement, you are loosing the pointer to the previously allocated UIImage you assigned in picture in the first lines, but you never released it.

You shouldn't alloc+init a new UIImage for picture, as you never use it and assign a new value to this variable later... but never used and released the previously allocated one.

财迷小姐 2024-12-11 21:47:34
  1. UIImage *picture = [[UIImage alloc] init];picture = UIGraphicsGetImageFromCurrentImageContext();
    您不应在代码开头初始化pictureUIGraphicsGetImageFromCurrentImageContext 返回已初始化的 UIImage(自动释放)。

  2. UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];图片=有效负载;[有效负载释放];
    您应该使用[payload autorelease],否则您将释放图像而不使用它。

  3. [委托 ThumbDidLoad:self.indexPathInTableView Image:[图片自动释放]];
    您应该删除[图片自动释放]并仅使用图片

    -(void)connectionDidFinishLoading:(NSURLConnection *)connection{
        UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];
        UIImage *图片;
        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);
            [有效负载drawInRect:imageRect];
            图片 = UIGraphicsGetImageFromCurrentImageContext();
            UIGraphicsEndImageContext();
        }
        别的
        {
            图片=有效负载;
        }
        [有效负载自动释放];
        self.activeDownload = nil;
    
        self.imageConnection = nil;
    
        [委托 ThumbDidLoad:self.indexPathInTableView Image:图片];
    }
    
  1. UIImage *picture = [[UIImage alloc] init]; and picture = UIGraphicsGetImageFromCurrentImageContext();
    You should not initialize picture at the beginning of the code. UIGraphicsGetImageFromCurrentImageContext returns a already initialized UIImage (autoreleased).

  2. UIImage *payload = [[UIImage alloc] initWithData:self.activeDownload];, picture = payload;, [payload release];.
    You should use [payload autorelease] instead, otherwise you're releasing the image without ever using it.

  3. [delegate ThumbDidLoad:self.indexPathInTableView Image:[picture autorelease]];
    You should remove [picture autorelease] and just use 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];
    }
    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文