开发者

Why am I getting a EXC_BAD_ACCESS in a NSTimer selector?

开发者 https://www.devze.com 2023-01-01 01:41 出处:网络
I\'ve got quite a weird problem. To make it short, i\'ll write some pseudo-code: init: create a dictionary and insert n elements.

I've got quite a weird problem. To make it short, i'll write some pseudo-code:

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

So, for an unknown reason, I get an EXC_BAD_ACCESS when I do:

    [item function];

But I traced the address I got from the dictionary items and it's ok. The ref count of the items in the dictionary is still 1. The {release, dealloc} of the items in the dictionary aren't called. Everything seems fine. Also, to make it worst, it works for some items.

So, I'm wondering if there is a threading problem? or something else obscure?

The callstack is quite simple:

#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

So, any suggestion where to look would be appreciated.

--- Edit #1 ---

@Laurent: It's a typo of me rewriting the actual value to match my example. (fixed)

@Jeremy: I will try to post some code to help. The code has been simplified.

Timer Init + Refresh Function:

_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];
     }
 }

I hope it's a little bit clearer.

right, I've commented everything in my "function" and it looks like it doesn't crash. I'll let it run a little more but I'm not doing anything special in that function (memory related). Just updating some enum values.

--- Edit #2 ---

@Laurent: you are right about the callstak, I made a huge mistake. It should be the timer method and not the function. I just fix it. Sorry for the mistake. But FYI, the method signature is:

- (bool)update;


I think I finally found what was the problem. In the method "update"

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

The problem was that I didn't do a retain on the date (creation) in the init. I don't really understand why the object got "corrupted", but I think the debugger should had pointed that variable and not the function call...

I'll let the application run for a while to see if the crash is gone.

Thanks for the help.

0

精彩评论

暂无评论...
验证码 换一张
取 消