why is my dealloc not called?

Discussion in 'iOS Programming' started by Tex-Twil, Jul 19, 2012.

  1. Tex-Twil, Jul 19, 2012
    Last edited: Jul 19, 2012

    Tex-Twil macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
    #1
    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
     
  2. Ides macrumors member

    Joined:
    Mar 27, 2012
    #2
    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.
     
  3. Tex-Twil thread starter macrumors 68020

    Tex-Twil

    Joined:
    May 28, 2008
    Location:
    Europe
    #3
    ok thanks!
     

Share This Page