使用 ARC 时 DCRoundSwitch 给出 EXC_BAD_ACCESS
我在一个项目中使用 DCRoundSwitch
,我基本上需要一个 UISwitch
我可以在其中编辑其标签内容。
因为我使用的是 ARC
,所以我将 DCRoundSwitch
代码重构为在 xcode 中与 ARC
兼容。
当编译并在模拟器中运行时,它可以正常工作。
但是,当在设备上运行时,它在 DCRoundSwitchKnobLayer.m
第 57 行附近给了我 EXC_BAD_ACCESS
有一个 错误报告在 GitHub,但尚未找到解决方案。
以下是提供 EXC_BAD_ACCESS
的代码:
CGGradientRef CreateGradientRefWithColors(CGColorSpaceRef colorSpace, CGColorRef startColor, CGColorRef endColor)
{
CGFloat colorStops[2] = {0.0, 1.0};
CGColorRef colors[] = {startColor, endColor};
//THIS LINE BREAKS THE PROGRAM
CFArrayRef colorsArray = CFArrayCreate(NULL, (const void**)colors, sizeof(colors) / sizeof(CGColorRef), &kCFTypeArrayCallBacks);
CGGradientRef gradient = CGGradientCreateWithColors(colorSpace, colorsArray, colorStops);
CFRelease(colorsArray);
return gradient;
}
任何线索将不胜感激。
编辑:以下是 xcode 中的局部变量:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我也遇到了同样的问题,我真的很想将代码转换为 ARC。
发生崩溃的原因是调用 CreateGradientRefWithColors 时 startColor 和 endColor 已被释放。
这是我的修复:
这篇文章有助于找到解决方案:http://www.bobmccune.com/2012/02/28/a-funny-thing-happened-on-the-way-to-the-arc/
I had the same problem and I really wanted to convert the code to ARC.
The crash occurs because startColor and endColor are already freed when CreateGradientRefWithColors is called.
Here is my fix:
This post helped in finding the solution: http://www.bobmccune.com/2012/02/28/a-funny-thing-happened-on-the-way-to-the-arc/
编辑
抱歉,使用 ARC 对此代码的正确修复是:
您使用颜色 NSArray 的桥接转换,而不是进行创建 CFArrayRef 的额外工作。这种重构是无耻地从 Core Graphics 101 的讨论中窃取的。这是一个很棒的 Objective-C 教程网站。
有关 ARC 的所有内容关于此的一些指南。 ARC 问答 对此也有一些很好的指南。
EDIT
Apologies, the correct fix for this code with ARC is:
You use a bridge conversion from an NSArray of the colors, rather than going to the extra work of creating the the CFArrayRef. This refactoring is shamelessly stolen from the Discussions on Core Graphics 101. Which is an awesome site of turorials on Objective-C.
The Everything you wanted to know about ARC has a few guides about this. The ARC Q&A also has some good guides on this.
colorsArray
使用CFRelease
方法释放。 ARC 不允许使用retain
和release
,所以为什么不尝试注释一下CFRelease(colorsArray)
行,看看是否可以这可以防止错误?编辑--
这个答案被接受,因为它通过禁用 ARC 为 OP 的问题提供了有效的解决方案。
如果您不想使用此方法,请参阅@petesh的回答。
colorsArray
is released using the methodCFRelease
. ARC doesn't allow the use ofretain
andrelease
, so why don't you try commenting the lineCFRelease(colorsArray)
and see if that prevents the error?Edit--
This answer was accepted as it supplied a valid fix to the OP's issue, by disabling ARC.
If you do not want to use this method, please see @petesh's answer.