没有 CFRelease 就无法运行,但没有它就会变成僵尸

发布于 2024-12-29 20:01:29 字数 1171 浏览 1 评论 0原文

在这段代码中:

            url = [NSURL fileURLWithPath:t.exportedFullName];

            myImageDest = CGImageDestinationCreateWithURL((CFURLRef)url, (CFStringRef)t.asset.defaultRepresentation.UTI, 1, nil);

            if(!myImageDest) {
                NSLog(@"***Could not create image destination ***");
            }
            inImage = t.asset.defaultRepresentation.fullResolutionImage;

            CGImageDestinationAddImage(myImageDest, inImage, (CFDictionaryRef)t.freshMetaData);
            success = CGImageDestinationFinalize(myImageDest);
            if(!success)
                NSLog(@"***Could not create data from image destination ***");

            [url release];
            CGImageRelease(inImage);
            CFRelease(myImageDest);

如果我不使用 CFRelease(myImageDest); ,它将在图像信息数组 (t) 中循环运行;我收到内存不足的警告,应用程序最终死掉(在设备上运行时)。

如果我使用它,我会得到:

-[Not A Type _cfTypeID]: message sent to deallocated instance

and profiling with Zombies in Instruments 显示 [UIImage dealloc] ,refcnt 为 -1,在其上方的行上显示 CFRelease,refcnt 为0 在上面这个类中。显示的 CFRelease 是该类中唯一的一个。

无法忍受它,没有它就无法生活......让我发疯......

有什么想法吗?

In this code:

            url = [NSURL fileURLWithPath:t.exportedFullName];

            myImageDest = CGImageDestinationCreateWithURL((CFURLRef)url, (CFStringRef)t.asset.defaultRepresentation.UTI, 1, nil);

            if(!myImageDest) {
                NSLog(@"***Could not create image destination ***");
            }
            inImage = t.asset.defaultRepresentation.fullResolutionImage;

            CGImageDestinationAddImage(myImageDest, inImage, (CFDictionaryRef)t.freshMetaData);
            success = CGImageDestinationFinalize(myImageDest);
            if(!success)
                NSLog(@"***Could not create data from image destination ***");

            [url release];
            CGImageRelease(inImage);
            CFRelease(myImageDest);

Which runs in a loop loopping through an array of image information (t) if I don't use the CFRelease(myImageDest); I get low memory warnings and the app eventually dies (when running on the device).

If I use it I get:

-[Not A Type _cfTypeID]: message sent to deallocated instance

and profiling with Zombies in instruments shows [UIImage dealloc] with a refcnt of -1 and on the line just above it shows CFRelease with a refcnt of 0 in this class above. The CFRelease shown is the only one in the class.

Can't live with it, can't live without...makin' me crazy...

Any ideas?

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

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

发布评论

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

评论(2

一个人的旅程 2025-01-05 20:01:29

有问题的行是:

 CGImageRelease(inImage);

不需要这样做,因为 defaultRepresentation.fullResolutionImage 不会增加返回的 CGImage 的保留计数。

此外,出于同样的原因,也不需要 [url release]

The offending line is:

 CGImageRelease(inImage);

This shouldn't be needed because defaultRepresentation.fullResolutionImage does not increase the retain count of the returned CGImage.

Also, [url release] is not needed for the same reason.

你与昨日 2025-01-05 20:01:29
url = [NSURL fileURLWithPath:t.exportedFullName];

然后后来

[url release];

现在情况很糟糕。您不使用 -alloc/-init、-copy、-mutableCopy 或 -retain url,因此您不拥有它。这意味着,释放它会导致 NSURL 实例的过度释放。因此,可能的情况是您的 CGImageRef 在内存中占据了它的位置,然后...

删除该版本和 CFRelease() 函数调用。出于同样的原因,这是没有必要的。

url = [NSURL fileURLWithPath:t.exportedFullName];

then later

[url release];

Now that's bad. You don't -alloc/-init, -copy, -mutableCopy nor -retain url, so you don't own it. That means, releasing it results in an overrelease of the NSURL instance. So, it's maybe the case that your CGImageRef takes its place in memory, and then...

Remove that release and the CFRelease() function call as well. For the same reason, it's unneccessary.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文