View Full Version : How Do You Exit A Thread To Avoid Memory Leaks

May 5, 2009, 11:25 AM

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];

May 5, 2009, 11:42 AM
Do you mean:

NSThread *thread = // The thread you defined above.
[thread exit];



In particular, see the cancel and exit methods.

May 5, 2009, 12:23 PM
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.

May 5, 2009, 12:50 PM
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.

Jan 26, 2010, 02:12 AM
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.

Jan 26, 2010, 12:36 PM
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.