0
感谢hotpaw2解决我的增量绘图问题。唉,我在解决方案中发现了一个问题。当我按下iphone底部的按钮暂停应用程序并重新启动时,它会将自身显示为内存引起的崩溃。 (只要我不这样做,我可以无限期地运行应用程序)。碰撞发生在低于所指示的线,这发生在FooBar的,UIView的的一个子类:CALayer崩溃,内存问题
- (void)drawRect:(CGRect)rect
{
CGContextRef ctx = UIGraphicsGetCurrentContext();
// @@CRASH here: Program received signal EXC_BAD_ACCESS
[backingLayer renderInContext:ctx]; // Render the backing layer into current context
[self randomRectangle: ctx];
}
在Foobar的接口I已宣布背衬层作为实例变量:
@private
NSTimer* timer;
CALayer *backingLayer;
在实施FooBar的的,backingLayer只发生一次,在initWithFrame
,下段:
backingLayer = self.layer;
// [backingLayer retain];
// Set its color space and background color
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
CGFloat components[4] = {0.0f, 0.0f, 0.0f, 1.0f};
CGColorRef bgColor = CGColorCreate(colorSpace, components);
backingLayer.backgroundColor = bgColor;
CGColorRelease(bgColor);
CGColorSpaceRelease(colorSpace);
我还应该说,有一个NSTimer timer
是在initWithFrame
火灾定义断tick
方法下方的第二12次:
- (void)tick
{
// Tell the view that it needs to re-draw itself
if (running) { // Go!
// NSLog(@"frameRate: %2.1f, frameCount: %d", frameRate, frameCount);
[self setNeedsDisplay];
frameCount++;
}
}
我已经运行仪器上这个程序。没有检测到内存泄漏。但是,当我运行Allocations并(a)用物理按钮挂起应用程序时,(b)触摸应用程序图标使其再次运行,然后看到大量的分配(〜2 GB)---和CRASH。
- Jim
我修复了这个问题。在UIView类中有一个布尔变量`running',当应用程序变为休眠状态时,它将被设置为FALSE,然后当它变为活动状态时将其设置为TRUE。 `tick`方法中的变量'running`决定是否调用`setNeedsDisplay`。 (调用`tick`的计时器也需要失效)。 对不起,不知道如何标记为已回答。 – 2011-01-29 15:42:55