How Do You Exit A Thread To Avoid Memory Leaks

Discussion in 'iOS Programming' started by happybob, May 5, 2009.

  1. macrumors newbie

    Joined:
    Apr 18, 2009
    #1
    Hi,

    I have one final 3.5 kb memory leak that's driving me insane. It's resulting from a thread not being exited before the view is released. The following is how I created the thread, what I'm looking for is a command like [something exit] or [something cancel] that will exit the thread right before the view is released. Thanks for the help.

    Code:
       // Created Using the following
    [NSThread detachNewThreadSelector:@selector(changeColor) toTarget:self withObject:nil];
    
    -(void) changeColor {
    NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
    
    //Actions
    
    [pool release];
    }
    
     
  2. macrumors 6502a

    Joined:
    Oct 24, 2008
    Location:
    Cambridge
    #2
  3. thread starter macrumors newbie

    Joined:
    Apr 18, 2009
    #3
    Actually I tried that by defining altThread in the header

    Code:
    NSThread *tempThread = [NSThread detachNewThreadSelector:@selector(changeColor) toTarget:self withObject:nil];
    
    altThread = tempThread
    
    Then I used the [altThread exit]; where I wanted it to exit.

    The problem was that I received an error "void value not ignored as it ought to be. So I tried a few other ways to create threads and either got warnings which crashed the app or an app that no longer worked as the thread no longer did what it was supposed to.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    detachNewThreadSelector:toTarget:withObject: returns void. If you want to store a reference to the NSThread object you need to use one of the init methods provided instead.
     
  5. macrumors newbie

    Joined:
    Sep 25, 2008
    Location:
    LA CA
    #5
    Solution?

    Anyone find a solution to this one? I'm honing in on it myself, and it'd be a big help if it were here for future trekkers.
     
  6. macrumors 603

    Joined:
    Jul 29, 2003
    Location:
    Silicon Valley
    #6
    Have the thread, after it's cleaned up it's own memory and just before it exits, send a message that the view can be released.
     

Share This Page