CADisplayLink 吞掉异常

发布于 2024-10-31 10:25:49 字数 730 浏览 1 评论 0原文

我注意到,使用 CADisplayLink 时,异常会被吞掉:

CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(doIt)];
[aDisplayLink setFrameInterval:100];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

...

- (void) doIt
{
    NSLog(@"Before");

    // Force an exception
    NSLog(@"%@", [[NSArray array] objectAtIndex:1]);

    NSLog(@"After");
}

运行此代码会产生以下输出:

2011-04-11 18:30:36.001 TestFrameLink[10534:207] Before
2011-04-11 18:30:37.666 TestFrameLink[10534:207] Before
2011-04-11 18:30:39.333 TestFrameLink[10534:207] Before

这是 CADisplayLink 的正确行为吗? 有没有什么方法可以让它在异常出现时中止程序,而不是展开堆栈并假装什么也没发生?

I've noticed that when using CADisplayLink, exceptions just get swallowed:

CADisplayLink *aDisplayLink = [[UIScreen mainScreen] displayLinkWithTarget:self selector:@selector(doIt)];
[aDisplayLink setFrameInterval:100];
[aDisplayLink addToRunLoop:[NSRunLoop currentRunLoop] forMode:NSDefaultRunLoopMode];

...

- (void) doIt
{
    NSLog(@"Before");

    // Force an exception
    NSLog(@"%@", [[NSArray array] objectAtIndex:1]);

    NSLog(@"After");
}

Running this code produces the following output:

2011-04-11 18:30:36.001 TestFrameLink[10534:207] Before
2011-04-11 18:30:37.666 TestFrameLink[10534:207] Before
2011-04-11 18:30:39.333 TestFrameLink[10534:207] Before

Is this the correct behavior for CADisplayLink?
Is there any way to make it abort the program when an exception bubbles up rather than unwind the stack and pretend nothing happened?

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

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

发布评论

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

评论(1

黎夕旧梦 2024-11-07 10:25:49

我怀疑 CoreAnimation 的 C++ 内部结构(如回溯中所证明的)是造成您注意到的异常吞没的原因(或者更确切地说,我不认为 N​​STimer 吞没异常); CoreAnimation 回调(在动画时从 +[UIView setAnimationDidFinishSelector:] 甚至可能 -viewDidAppear: 调用)似乎做了同样的事情,除了我认为它们打印了一条日志消息。我不知道为什么苹果选择异常吞咽而不是异常处理;那好吧。

据我所知,做你所要求的事情的唯一方法是,

@try
{
  ...
}
@catch(...)
{
  abort();
}

没有太大帮助,我知道。另外两件事可能会有所帮助:

I suspect CoreAnimation's C++ innards (as evidenced in backtraces) are responsible for the exception-swallowing you've noticed (or rather, I don't think NSTimer swallows exceptions); CoreAnimation callbacks (called from +[UIView setAnimationDidFinishSelector:] or probably even -viewDidAppear: when animated) seem to do the same thing, except I think they printed a log message. I'm not sure why Apple's chosen exception-swallowing in preference to exception handling; oh well.

The only way to do what you're asking is, as far as I know,

@try
{
  ...
}
@catch(...)
{
  abort();
}

Not much help, I know. Two other things that might help:

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