簡體   English   中英

為什么在NSTimer選擇器中得到EXC_BAD_ACCESS?

[英]Why am I getting a EXC_BAD_ACCESS in a NSTimer selector?

我有一個很奇怪的問題。 簡而言之,我將編寫一些偽代碼:

init: create a dictionary and insert n elements.
      create a "repeat timer" and add it to the currentRunLoop using the timerRefresh selector.

timerRefresh: using a list of keys, find the items in the dictionary
              if the item exists -> call a function

因此,出於未知的原因,我在執行以下操作時會得到EXC_BAD_ACCESS:

    [item function];

但是我跟蹤了從字典項中獲得的地址,沒關系。 字典中各項的引用計數仍為1。不調用字典中各項的{release,dealloc}。 一切似乎都很好。 另外,最糟糕的是,它適用於某些物品。

因此,我想知道是否存在線程問題? 還是其他晦澀的東西?

調用棧非常簡單:

#0  0x93e0604b in objc_msgSend_fpret
#1  0x00f3e6b0 in ??
#2  0x0001cfca in -[myObject timerRefresh:] at myObject.m:000
#3  0x305355cd in __NSFireTimer
#4  0x302454a0 in CFRunLoopRunSpecific
#5  0x30244628 in CFRunLoopRunInMode
#6  0x32044c31 in GSEventRunModal
#7  0x32044cf6 in GSEventRun
#8  0x309021ee in UIApplicationMain
#9  0x000027e0 in main at main.m:14

因此,任何建議在哪里尋找將不勝感激。

-編輯#1-

@Laurent:這是我改寫實際值以匹配我的示例的錯字。 (固定)

@傑里米:我將嘗試發布一些代碼來提供幫助。 該代碼已簡化。

計時器初始化+刷新功能:

_refreshTimer = [[NSTimer alloc] initWithFireDate:[NSDate dateWithTimeIntervalSinceNow:5] interval:5
                                 target:self selector:@selector(onTimerRefresh:) userInfo:nil repeats:YES];
[[NSRunLoop currentRunLoop] addTimer:_refreshTimer forMode:NSDefaultRunLoopMode];

//...

 (void)onTimerRefresh:(NSTimer*)theTimer {
      // the actual code is here. I will rewrite it so it's simpler:
     for (MyKey key in keys) {
         MyObject* object = [dictionary objectForKey:key];
         if (object)
             [object function];
     }
 }

我希望它更清晰一些。

正確,我已經在“功能”中注釋了所有內容,看起來好像沒有崩潰。 我會讓它再運行一點,但是在該功能(與內存相關的功能)中,我沒有做任何特別的事情。 只是更新一些枚舉值。

-編輯2-

@Laurent:你對calltak說的沒錯,我犯了一個很大的錯誤。 它應該是計時器方法,而不是函數。 我解決了 對不起,這個錯誤。 但僅供參考,方法簽名為:

- (bool)update;

我想我終於找到了問題所在。 在方法“更新”中

- (bool)update {
    // ...
    NSDate* now = [NSDate dateWithTimeIntervalSinceNow:0];
    NSTimeInterval interval = [now timeIntervalSinceNow] - [creation timeIntervalSinceNow];
    //...
}

問題是我沒有保留init中的日期(創建)。 我不太了解為什么對象“損壞”,但是我認為調試器應該指出變量而不是函數調用...

我將讓應用程序運行一段時間,以查看崩潰是否消失。

謝謝您的幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM