没有 CFRelease 就无法运行,但没有它就会变成僵尸
在这段代码中:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
有问题的行是:
不需要这样做,因为
defaultRepresentation.fullResolutionImage
不会增加返回的 CGImage 的保留计数。此外,出于同样的原因,也不需要
[url release]
。The offending line is:
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.然后后来
现在情况很糟糕。您不使用 -alloc/-init、-copy、-mutableCopy 或 -retain
url
,因此您不拥有它。这意味着,释放它会导致 NSURL 实例的过度释放。因此,可能的情况是您的 CGImageRef 在内存中占据了它的位置,然后...删除该版本和 CFRelease() 函数调用。出于同样的原因,这是没有必要的。
then later
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.