I’m using a third-party Objective-C library that makes a web request in a background thread, then returns the result by using [self performSelectorOnMainThread:...] which then calls a delegate method. I understand that I need to nil the delegate reference before releasing the delegate, but I was wondering what happens if this requesting object itself gets deallocated while the background thread is running. Will this internal self reference get set to nil so that the -performSelectorOnMainThread: call is harmless, or is there a potential for a crash here?
I’m using a third-party Objective-C library that makes a web request in a background
Share
As far as I understand your scenario (but possibly you should include some code), the statement:
should be the last one to be executed in your thread (since it is the way to return the result of your thread, it is still part of the thread selector passed to
NSThread).If it is reasonably so, then consider that when you first detach an
NSThread, you pass it atargetobject (yourself) and theNSThreadwillretainit as long as the passedselectorhasn’t completed. This will include your[self performSelectorOnMainThread:...], so, unless someone messes heavily withreleases, there should be no chance forselfto be deallocated before[self performSelectorOnMainThread:...]is executed.If your question was exactly what happens if someone messes with
releases, I will give this a second thought.