【问题标题】:Crash with CALayer, memory problemCALayer崩溃,内存问题
【发布时间】:2011-01-25 08:07:59
【问题描述】:

感谢 hotpaw2 解决了我的增量绘图问题。唉,我在实施该解决方案时发现了一个问题。当我按下 iphone 底部的按钮暂停应用程序然后重新启动它时,它显示为内存引起的崩溃。 (只要我不这样做,我就可以无限期地运行该应用程序)。崩溃发生在下面指示的行,它发生在 UIView 的子类 FooBar 中:

- (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 的界面中,我已将支持层声明为实例变量:

@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);

我还应该说,initWithFrame 中定义的 NSTimer timer 每秒会触发低于 12 次的 tick 方法:

- (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++;
    } 
}

我已经在这个应用上运行了 Instruments。未检测到内存泄漏。但是当我运行 Allocations 并 (a) 使用物理按钮暂停应用程序时,(b) 触摸应用程序图标以使其再次运行,然后我看到巨大的分配集群 (~2 GB) --- 和崩溃。

-- 吉姆

【问题讨论】:

  • 我已经修好了这个。 UIView 类中有一个布尔变量running,当应用程序处于休眠状态时设置为 FALSE,然后在应用程序变为活动状态时设置为 TRUE。 tick 方法中的变量running 决定是否调用setNeedsDisplay。 (调用tick的定时器也需要失效)。抱歉,不知道如何将此标记为已回答。

标签: iphone memory crash calayer


【解决方案1】:

请参阅我应该放在这里的最后一条评论。通过在应用程序进入其非活动状态时将BOOL running 设置为 FALSE 来修复此问题,当它再次变为活动状态时将其重置为 TRUE。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-02
    • 2017-01-27
    • 2013-02-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-09
    相关资源
    最近更新 更多