views:

30

answers:

1

I've got a repeating NSTimer that I invalidate manually some time before I dealloc the object that owns the callback that is invoked by the timer. I verify that when I invalidate the timer, the callback is not being called anymore. When the object deallocs, an instant later I get an EXC_BAD_ACCESS. The crash is correlated with whether I invalidate the timer or not, i.e. if I don't invalidate it, there is no crash.

Does anyone know why this might be happening? I don't see why the timer would try to access the callback for the dealloc'ed object, which is what seems to be happening. I'm at a loss how to debug this further. The callstack just says:

#0  0x02f05c93 in objc_msgSend
#1  0x00000001 in ??
#2  0x02d255fa in CFRunLoopRunSpecific
#3  0x02d248a8 in CFRunLoopRunInMode
#4  0x037de89d in GSEventRunModal
#5  0x037de962 in GSEventRun
#6  0x00863372 in UIApplicationMain
#7  0x00002e40 in main at main.m:13

UPDATE: I have determined it is not the timer but a leak resulting from my parent object calling dealloc (the non-invalidating timer was preventing dealloc from calling). It would still be useful to hear advice on how to debug things when I hit a wall with the callstack, if it is even possible, so I'll leave this question up.

+1  A: 

When you invalidate an NSTimer, the receiver sends a release to the target and userInfo parameters of timerWithTimeInterval:target:selector:userInfo:repeats:. Is it possible you are referring to these objects or have some incorrect memory-management with one of these objects?

One way to debug this is to make use of NSZombie.

greg
Thank you for the pointer to NSZombie. I discovered the problem wasn't the NSTimer after all, but rather the fact that when I *properly* invalidate them all, my object is able to be dealloc'ed (instead of retained by the timer) and the dealloc itself was causing the crash because of another object.
Joey
Glad you were able to correct the situation. Feel free to upvote and/or accept the answer to help others if they encounter this situation. Remember, zombies are your friends...at least NSZombies!
greg