CGImageGetColorSpace(image)的结果是否必须被释放?
我正在缩放CGImageRef
。我在网上找到了各种代码示例,开头如下:
CGColorSpaceRef colorspace = CGImageGetColorSpace(image); // "Get" color space
CGContextRef context = CGBitmapContextCreate(NULL, width, height,
CGImageGetBitsPerComponent(image),
CGImageGetBytesPerRow(image),
colorspace,
CGImageGetAlphaInfo(image));
CGColorSpaceRelease(colorspace); // Really?
正如您在上面看到的,颜色空间
已发布。然而,当我这样做时,我的代码大部分时间都可以工作,但偶尔会崩溃,因为有时该颜色空间实例已经消失了。 API 文档说:
您有责任根据需要保留和释放色彩空间。
这是否意味着我必须释放它?我假设约定是只有名称中带有“Create
”的调用结果才会返回必须显式释放的对象。这是否意味着网络上的示例在发布该 CGColorSpaceRef
时完全是错误?
I'm scaling a CGImageRef
. I found various code examples on the web that begin like so:
CGColorSpaceRef colorspace = CGImageGetColorSpace(image); // "Get" color space
CGContextRef context = CGBitmapContextCreate(NULL, width, height,
CGImageGetBitsPerComponent(image),
CGImageGetBytesPerRow(image),
colorspace,
CGImageGetAlphaInfo(image));
CGColorSpaceRelease(colorspace); // Really?
As you can see above, the colorspace
is released. However, when I do that my code works most of the time, but crashes once in a while, because sometimes that colorspace instance is already gone. The API docs say:
You are responsible for retaining and releasing the color space as necessary.
Does that mean I must release it? I assumed the convention was that only results of calls with "Create
" in the name return objects that have to be released explicitly. Does this mean the examples on the web are simply wrong when releasing that CGColorSpaceRef
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
正如 API 文档所述,您有责任根据需要保留和释放颜色空间。也就是说,如果您需要,请保留它。如果不保留,就不要释放。
在此处了解更多信息
As the API docs say, you are responsible for retaining and releasing the color space as necessary. I.e. if you need, retain it. If you do not retain, don't release.
Read more about it here
请注意,释放颜色空间对象似乎可能会导致崩溃。我做了一些挖掘,但无法确定崩溃的根本原因。
我们有一个应用程序,它从它处理的每个位图的颜色空间中读取字段,并调用 [UIImage imageName],然后调用 CGImageGetColorSpace,然后调用 CGColorSpaceRelease。在特定场景中,它会对同一张图像连续多次执行此操作。不确定的是,这有时会在 CGColorSpaceRelease 期间崩溃并出现以下错误:
这是在 iOS5 上的 ipad 和 sim 上发生的。
我知道,这是最糟糕的错误报告,但是嘿,如果您遇到同样的问题并开始抓狂(我们就是这样做的),那么也许这可以确认您不是唯一一个遇到这种情况的人打击这种行为。
此错误:“运行 WebGL 一致性测试时随机崩溃” 似乎就像这可能是同一个问题。或者这个。
我们的解决方法是停止读取色彩空间数据。仍然不太确定出了什么问题。抱歉,希望我能提供更多信息
--- Dave
p.s.,http://xkcd.com/979/ ,并对“这里有龙”的帖子表示歉意。 :)
Note that it seems possible to get a crash from releasing the colorspace object. I did some digging, but was unable to ascertain the root cause of the crash.
We have an app that was reading fields from the colorspace of each bitmap it dealt with and would call [UIImage imageName] followed by CGImageGetColorSpace followed by CGColorSpaceRelease. And in a certain scenerio, it would do this multiple times in a row for the same image. Non-deterministically, this would sometimes crash with the following error during the CGColorSpaceRelease:
This was on iOS5 on both the ipad and the sim.
I know, this is the worst kind of bug report, but hey, if you run into the same issue and start pulling your hair out (we did), then perhaps this can serve as confirmation that you aren't the only person to ever hit this behavior.
This bug: "random crash while running WebGL conformance tests" seems like it might be the same issue. Or this one.
Our workaround was to stop reading the colorspace data. Still not quite sure what was going wrong. Sorry, wish I could give more info
--- Dave
p.s., http://xkcd.com/979/, and sorry for the "here be dragons" post. :)
同样的问题曾经发生在我身上。我发现,在我的情况下,问题是释放以下 ColorSpace:
在我将该行更改为:之后,
一切都像以前一样工作,除了错误:)
The same problem used to happen to me. I found out, that in my case the problem was the releasing the following ColorSpace:
After i changed that line to:
everything worked as before, except of the error :)
管理 CoreFoundation 对象生命周期和其他类似对象(例如 CoreGraphic 对象)的规则是,如果您使用名称中包含
Create
或Copy
的函数获取该对象,则当您使用完对象后,需要使用适当的释放方法。由于原始问题中的代码显示 CoreGraphics 对象是使用名称中带有
Get
的方法获得的,而不是Create
或Copy
那么你使用后不应释放该对象。如果你确实发布了,你应该预料到会发生崩溃。The rule for managing CoreFoundation objects lifetimes and other objects like them e.g., CoreGraphic objects is that if you have obtained the object using a function that has
Create
orCopy
in its name then you need to use the appropriate release method when you have finished with the object.Since the code in the original question shows that the CoreGraphics object was obtained using a method with
Get
in its name and notCreate
orCopy
then you should NOT release the object after using it. If you do release you should expect to get crashes.是的,你必须这样做。
从
CGImageGetColorSpace
返回的CGColorSpaceRef
不会被保留,正如其名称中的“Get”所暗示的那样。您可以决定是否调用CGColorSpaceRetain(colorSpace)
。但是,如果您打算对该颜色空间做任何有用的事情(例如创建渐变或其他内容),我强烈敦促您这样做(当然,稍后发布)。如果你不这样做,你就会陷入各种 CG 功能随机崩溃的噩梦。
如果您需要将某些 CG 对象传递给其他函数,请始终确保保留它。总是。
Yes, you have to.
The returned
CGColorSpaceRef
fromCGImageGetColorSpace
is not retained, as its "Get" in the name implies. And it's your call will you then callCGColorSpaceRetain(colorSpace)
or not.However, I strongly urge you to do that (and later release, of course) if you intend to do anything useful with that color space (say create a gradient or something). If you do not, you will invite yourself to a nightmare of random crashes in various CG functions.
If you need some CG object to be passed to some other functions, always make sure it's retained. Always.