【问题标题】:CGContextRef - EXC_BAD_ACCESS Error [duplicate]CGContextRef - EXC_BAD_ACCESS 错误 [重复]
【发布时间】:2012-09-02 16:12:14
【问题描述】:

我正在继承 UIView 并使用它的实例来设置我的 UITableViewCell backgroundView 和 selectedBackedView 属性。我在 UIView 子类的 drawRect 方法中收到 EXC_BAD_ACCESS 错误。

    if(nil == cell){

    cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault
                                 reuseIdentifier:CellIdentifier];
    cell.backgroundView = [[CCViewBackground alloc]init];
    cell.selectedBackgroundView = [[CCViewBackground alloc]init];

    }

UIView 子类 CCBackgroundView -drawRect:

- (void)drawRect:(CGRect)rect
{
     // Drawing code
     CGContextRef context = UIGraphicsGetCurrentContext();

     CGColorRef redColor = 
     [UIColor colorWithRed:1.0 green:0.0 blue:0.0 alpha:1.0].CGColor;

     CGContextSetFillColorWithColor(context, redColor); //Receiving EXC_BAD_ACCESS here
     CGContextFillRect(context, self.bounds);

}

【问题讨论】:

  • 能否为您的应用启用 NSZombie 并分享收到 EXC_BAD_ACCESS 的原因
  • 这是现在启用 NSZombie 的错误消息 - *** -[Not A Type retain]: message sent to deallocated instance 0x109b50

标签: iphone ios cocoa-touch core-graphics


【解决方案1】:

我假设您使用的是 ARC。如果是这样,您将遇到一个众所周知的问题,即CGColorRef 的发布时间比您预期的要早。 This article详细解释了问题并提供了几种解决方案。

【讨论】:

  • 是的,我正在使用 arc。感谢您的文章,它解决了问题
  • 赞美互联网!这解决了它,感谢您的文章!
  • 有人可以再分享一下文章的链接吗...现在链接失效了。
猜你喜欢
  • 2012-08-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-02-26
  • 2012-04-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多