Deallocated instance exception with threads
I've a problem with an app for Mac that I'm writing in Objective-c. I've this situation:
- In main thread (GUI):
ftEngine = [[FileT alloc] init]; [ftEngine setParameters:searchWord selectedEngine:[[pbEngines selectedItem] title] actualPage:0]; NSThread* thFileT = [[NSThread alloc] initWithTarget:ftEngine selector:@selector(setTotalResult) object:nil]; [thFileT start];
- In child (ftEngine previous declared):
-(void)setTotalResult { NSError* nsError = nil; NSURL* urlCompleteUrl = [NSURL URLWithString:m_completeSearchWord]; }
m_completeSearchWord is initialized by setParameters function previously utilized.
And now.. my problem is: When thread is started, it call setTotalResult function and i'll get an exception when I try to use m_completeSearchWord.
It's strange becacuse开发者_StackOverflow社区 if I don't use a thread, all works correctly!
Exception is:
2011-09-08 23:24:06.731 GUI[12935:1a07] *** -[CFString respondsToSelector:]: message sent to deallocated instance 0x1003cc650
It sounds like you may not have retained m_completeSearchWord
correctly when you initialized it. Add the body of -setParameters
if you want help confirming that.
When calling selectors in a new thread, make sure that the selector has been properly wrapped with an autorelease pool :
-(void) setTotalResult {
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
...
[pool release];
}
I see all manner of memory related issues when I forget to add the pool and your error definitely rings a bell.
精彩评论