How Do You Exit A Thread To Avoid Memory Leaks

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

  1. happybob macrumors newbie

    Apr 18, 2009

    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.

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

    Oct 24, 2008
  3. happybob thread starter macrumors newbie

    Apr 18, 2009
    Actually I tried that by defining altThread in the header

    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. kainjow Moderator emeritus


    Jun 15, 2000
    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. beon macrumors newbie

    Sep 25, 2008
    LA CA

    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. firewood macrumors 604

    Jul 29, 2003
    Silicon Valley
    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