Vijay
Vijay

Reputation: 71

How to release a NSThread is there any particular process to release?

In my code for search controller I am using NSthread. For every character we enter in the search bar a new thread is created very next the thread is started. How do we release these NSThreads which are causing memory leaks?

Here is where my NSThreads are created:

if (searchString.length > 1) {
    self.searchThread = [[[NSThread alloc] initWithTarget:self selector:@selector(filterContentForSearchTextInBackgroundThread:) object:searchString] autorelease];
    [self.searchThread start];

}

Upvotes: 0

Views: 645

Answers (1)

bbum
bbum

Reputation: 162712

Answer: self.searchThread = nil; will release the thread when it is done executing.

Longer answer; you really really don't want to spawn a thread on every keystroke. If you are going to use threads at all, you should have ONE thread that processes keystrokes and you should tell that thread about the keystrokes as they happen.

Which means you are probably going to need some kind of "cancel the current query and start a new one" mechanism, too.

Upvotes: 4

Related Questions