1

我有一个很奇怪的问题。简而言之,我将编写一些伪代码:

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:这是我重写实际值以匹配我的示例的错字。(固定的)

@Jeremy:我会尝试发布一些代码来提供帮助。代码已被简化。

定时器初始化+刷新功能:

_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:关于callstak,你是对的,我犯了一个巨大的错误。它应该是计时器方法而不是函数。我只是修复它。对不起这个错误。但仅供参考,方法签名是:

- (bool)update;
4

1 回答 1

0

我想我终于找到了问题所在。在“更新”方法中

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

问题是我没有在初始化中的日期(创建)上进行保留。我真的不明白为什么对象会“损坏”,但我认为调试器应该指向该变量而不是函数调用......

我会让应用程序运行一段时间,看看崩溃是否消失了。

谢谢您的帮助。

于 2010-05-27T15:36:26.150 回答