RLH*_*RLH 8 exc-bad-access objective-c ipad ios ios-simulator
我知道如何解决我即将概述的问题,但是,我有点困惑的是为什么代码方案在iOS模拟器中工作但在我的iPad上不起作用.
我有一个检查各种属性的方法,然后CALayer根据属性的状态设置a的背景颜色.以下代码与我的颜色分配方法类似:
//This will be the CALayer BGColor...
CGColor c = UIColor.blueColor.CGColor; //Blue is the default
switch (myState)
{
case state_one:
c = UIColor.greenColor.CGColor;
//... more code ...
break;
case state_two:
c = UIColor.redColor.CGColor;
//... more code ...
break;
case state_three: //multiple cases are like the state_three case.
//Other code, but I don't need to assign the color. Blue works...
}
myCALayer.backgroundColor = c; //Oh-noes!!! Here we get the dreaded EXC_BAD_ACCESS on iPad
//...more code dealing with the layer.
Run Code Online (Sandbox Code Playgroud)
上面的代码在模拟器中没有问题.但是,当我在iPad上运行应用程序时,它会在backgroundColor分配时崩溃.
我可以通过摆脱CGColor变量并直接在我的switch/case语句中分配背景颜色来解决这个问题,这就是我正在计划做的事情.
但是,我很好奇.为什么这会在一个环境中工作而不在另一个环境中?
UPDATE
几件事.首先,值得一提的是,这是一个ARC项目,使用Xcode 4.2,针对iOS 5设备.此外,我的颜色分配代码并不完全是它的样子,因为我有一系列用于设置这些颜色的定义,因为它们在我的整个应用程序中被引用.
这是一些#define陈述的样子:
#define BLUE [UIColor colorWithRed:8.0/255.0 green:80.0/255.0 blue:150.0/255.0 alpha:1.0].CGColor
#define GREEN (UIColor.blueColor.CGColor)
//...and there are about 6 other colors
Run Code Online (Sandbox Code Playgroud)
我试图简化我的代码,因为编译器应该将refs替换为我的refs到我的定义.不过,为了以防万一,值得一提.
这是我的预感:UIColor创造它(并保持其唯一参考)的可能性在你通过之前已经被破坏了CGColor.由于CGColorRef在ARC下没有为您处理引用计数,如果UIColor在您使用之前销毁它的颜色将被摧毁,则颜色将是悬空参考CGColor.
ARC有一个优化,其中"自动释放"对象可能永远不会添加到自动释放池,而是released在不再引用objc对象之后.这是三件事的组合:
知道了,我怀疑这个程序会纠正这个问题:
UIColor * c = UIColor.blueColor; //Blue is the default
switch (myState) {
case state_one:
c = UIColor.greenColor;
//... more code ...
break;
case state_two:
c = UIColor.redColor;
//... more code ...
break;
case state_three: //multiple cases are like the state_three case.
//Other code, but I don't need to assign the color. Blue works...
}
myCGLayer.backgroundColor = c.CGColor;
//...more code dealing with the layer.
Run Code Online (Sandbox Code Playgroud)
更详细地说,编译器和objc运行时可以通过多种方式解释和执行您的程序.这意味着当您更改编译器版本或更新运行时(OS)时,此问题可能会影响您.当您使用的库使用不同版本或编译器设置更新或构建时,也会发生这种情况.例如:如果库在此过程中切换到ARC,它可能会使用不同的运行时调用,或者如果更新了编译器注入的调用,则调用可能会以不同方式使用线程本地数据.
有关与运行时相关的ARC规范的详细信息,请访问:http: //clang.llvm.org/docs/AutomaticReferenceCounting.html#runtime
这里也出现了类似的问题:
由于ARC,在方法结束时过早地释放颜色.
我用:CGColorRetain
CGColorRef whiteColor = CGColorRetain([UIColor colorWithRed:1.0 green:1.0
blue:1.0 alpha:1.0].CGColor);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
9239 次 |
| 最近记录: |