iPhone崩溃 - 消息发送到deallocated实例 [英] iPhone crash- Message sent to deallocated instance

查看:186
本文介绍了iPhone崩溃 - 消息发送到deallocated实例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好,这里是我得到的错误:

Ok, here is the error I am getting:

    -[CFRunLoopTimer invalidate]: message sent to deallocated instance 0x109b05a0 (gdb)

这是导致崩溃的代码:

    if (waitingOpponentTimer) {
      [waitingOpponentTimer invalidate]; //<-- Crash/error occurs here
      waitingOpponentTimer = nil;
    }

和其他地方:

    NSTimer* waitingOpponentTimer;

还有:

    waitingOpponentTimer = [NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO];

waitingOpponentTimer没有其他任何地方出现,只有上面显示的。 waitingOpponentTimeOut操作只是设置几个变量,对计时器或waitingOpponentTimer没有任何作用。

There is NO other occurence of waitingOpponentTimer anywhere, only the ones I have shown above. waitingOpponentTimeOut action just sets a few variables and does nothing to timers or to waitingOpponentTimer.

我已经尝试所有以下IF语句,以防止无效语句被访问: p>

I have tried all the following IF statements to prevent the invalidate statement from being reached:

    if (waitingOpponentTimer) {
    if ([waitingOpponentTimer retainCount] > 0) {
    if (waitingOpponentTimer.isValid) {
    if (waitingOpponentTimer != nil) {

但在所有情况下仍然得到通过IF语句,然后导致与invalidate语句的崩溃。

But in all cases, it still gets thru the IF statement and then causes the crash with the invalidate statement.

所以我的问题是,为什么/如何invalidate导致错误时,对象isValid和不是nil,并且它的retainCount大于零?

So my question is, why/how is invalidate causing an error when the object isValid and is not nil and it's retainCount is greater than zero?

是否有另一种方法来检查这是否阻止invalidate被访问?

Is there another way to check this to prevent invalidate from being reached?

我是一个完整的noob到iphone编程,并花了几个小时googling尝试,并计算出来,但不能弄清楚我做错了。任何帮助和建议赞赏!

I am a complete noob to iphone programming and have spent several hours googling to try and figure this out but cannot figure out what I am doing wrong. Any help and suggestions appreciated!

编辑:
为了澄清,我使用它来手动停止计时器之前运行。我明白,如果它用完了,它使自己无效,这就是为什么我使用IF语句检查它是否已经失效(但它不工作)。所以我认为当这个崩溃发生时,计时器已经失效了,但是如何检查它是否已经失效了?

Just to clarify, I am using this to manually stop the timer before it runs. I understand that if it runs out, it invalidates itself, which is why I am using the IF statement to check if it has already been invalidated (but it is not working). So I think the timer has already invalidated itself when this crash happens, but how do I check if it is already invalidated?

编辑:
Ok我标记了正确答案。我不知道是否有一个原因,当定时器没有保留并且已经使自己无效时,代码通过IF语句获取,但是答案是如果你要在IF语句中使用定时器,总是使用RETAIN

Ok I marked the correct answer below. I do not know if there is actually a reason why the code gets by the IF statements when the timer was not retained and has already invalidated itself, but the answer is to always use RETAIN if you are going to use the timer in an IF statement.

这里是相关信息:如果您不需要使NSTimer无效,是否有理由保留它?

推荐答案

您是否使用ARC?

waitingOpponentTimer = [[NSTimer scheduledTimerWithTimeInterval:10.0f target:self selector:@selector(waitingOpponentTimeOut)userInfo:nil repeats:NO] retain];

这篇关于iPhone崩溃 - 消息发送到deallocated实例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆