Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

Tex-Twil

macrumors 68030
Original poster
May 28, 2008
2,501
15
Berlin
Hi,
I have an object Player which sets up some timers that I need to invalidate in the dealloc method but I realized this method gets never called:

Code:
- (void)dealloc {
    if (statusTimer && statusTimer.isValid) {
        [statusTimer invalidate];
    }
}

I narrowed it down to a use case where I just load my players but do nothing with them so I though they should be released directly:

Code:
NSDictionary *searchResults = ... //get persisted data

for (NSArray *playerJsonArray in [searchResults allValues]) {
// here I allocate my player
Player *player = [self parsePlayerFromJSON:object];
// but do nothing with it!
}


This code allocates the player, my internal timers in the player are started but they keep on running because the dealloc method does not get called. Why? I do not assign the player object to anything.

btw, I am using ARC

Thanks
 
Last edited:
Ides

NSTimers retain their target objects, so your player object is being retained by the timers. Invalidating the timers in dealloc won't do anything because dealloc won't be called. You need to invalidate the timers before dealloc in order for dealloc to be called.
 
NSTimers retain their target objects, so your player object is being retained by the timers. Invalidating the timers in dealloc won't do anything because dealloc won't be called. You need to invalidate the timers before dealloc in order for dealloc to be called.

ok thanks!
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.