CALayer 崩溃、内存问题

发布于 2024-10-14 17:35:34 字数 1565 浏览 2 评论 0原文

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

- (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 计时器 会触发 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++;
    } 
}

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

——吉姆

My thanks to hotpaw2 for the solution to my incremental drawing problem. Alas, I discovered an issue with the solution as I implemented it. It reveals itself as a memory-induced crash when I press the botton at the bottom of the iphone to suspend the app and then restart it. (I can run the app indefinitely as long as I don't do this). The crash occurs at the indicated line below, which occurs in FooBar, a subclass of UIView:

- (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];

}

In the interface of FooBar I have declared backing layer as instance variable:

@private

NSTimer* timer;
CALayer *backingLayer;

In the implementation of FooBar, backingLayer occurs just once more, in initWithFrame, in the following paragraph:

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

I should also say that there is an NSTimer timer that is defined in initWithFrame fires off the tick method below 12 times a second:

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

I've run Instruments on this app. No memory leaks are detected. But when I run Allocations and (a) suspend app with physical button, (b) touch the app icon to make it run again, then I see huge cluster of allocations (~2 GB) --- and CRASH.

-- Jim

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

少女的英雄梦 2024-10-21 17:35:34

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

See last comment, which I should have put here. Fixed this by setting BOOL running to FALSE when the app enters its inactive state, resetting it to TRUE when it becomes active once again.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文