CALayer 崩溃、内存问题
感谢 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
请参阅最后一条评论,我应该把它放在这里。通过在应用进入非活动状态时将
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.