【发布时间】:2011-12-16 11:40:23
【问题描述】:
我正在使用 CATiledLayer 作为我的 UIView 的支持层,我将它放在 UIScrollView 中。在我的视图的 init 方法中,我正在创建绘制简单线条的 CGPathRef 对象。当我试图在 drawLayer:inContext 中绘制这条路径时,它偶尔会在我滚动/缩放时与 EXEC_BAD_ACCESS 崩溃(很少)。
代码很简单,我只使用标准的 CG* 函数:
- (id)initWithFrame:(CGRect)frame
{
self = [super initWithFrame:frame];
if (self) {
CATiledLayer *tiledLayer = (CATiledLayer *)[self layer];
tiledLayer.levelsOfDetail = 10;
tiledLayer.levelsOfDetailBias = 5;
tiledLayer.tileSize = CGSizeMake(512.0, 512.0);
CGMutablePathRef mutablePath = CGPathCreateMutable();
CGPathMoveToPoint(mutablePath, nil, 0, 0);
CGPathAddLineToPoint(mutablePath, nil, 700, 700);
path = CGPathCreateCopy(mutablePath);
CGPathRelease(mutablePath);
}
return self;
}
+ (Class) layerClass {
return [CATiledLayer class];
}
- (void) drawRect:(CGRect)rect {
}
- (void) drawLayer:(CALayer *)layer inContext:(CGContextRef)ctx {
CGContextSetRGBFillColor(ctx, 1, 1, 1, 1);
CGContextFillRect(ctx, self.bounds);
CGContextSetLineWidth(ctx, 5);
CGContextAddPath(ctx, path);
CGContextDrawPath(ctx, kCGPathStroke);
}
- (void)dealloc {
[super dealloc];
}
更新: 我注意到这个问题只存在于 iOS 5 上,它在 4.3 上运行良好
【问题讨论】:
-
启用 NSZombieEnabled 后,调试 EXEC_BAD_ACCESS 很容易,它会显示正在调用的已释放变量。
-
但是在这段代码中不可能对内存做坏事,无论如何它只会发生在 CATiledLayer 和多线程中。
-
这是线程共享的类还是每个线程都有自己的实例?
-
视图是在UI线程中创建的,但是drawayer方法是从不同的线程调用的,因为它被CATiledLayer使用,这就是它的工作方式。 BAD_ACCESS 总是出现在这一行:CGContextAddPath(ctx, path)
标签: iphone ios ipad catiledlayer cgpath