Created
November 27, 2011 13:28
-
-
Save steipete/1397553 to your computer and use it in GitHub Desktop.
Ugly as hell workaround for UIWebView crashing on non-main thread dealloc (inside a UIView)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)dealloc { | |
webView_.delegate = nil; // delegate is self here, so first set to nil before call stopLoading. | |
[webView_ stopLoading]; | |
// UIWebView must be released in the main thread, or we get: | |
// Tried to obtain the web lock from a thread other than the main thread or the web thread. This may be a result of calling to UIKit from a secondary thread. Crashing now... | |
// This is less evil than the proposed http://stackoverflow.com/questions/945082/uiwebview-in-multithread-viewcontroller | |
// Calling removeFromSuperview in a dealloc is ugly and evil, but else UIView has a strong reference to UIWebView and our main-release call would be irrelevant. | |
if (![NSThread isMainThread]) { | |
[webView_ performSelectorOnMainThread:@selector(removeFromSuperview) withObject:nil waitUntilDone:YES]; | |
} | |
[webView_ performSelectorOnMainThread:@selector(release) withObject:nil waitUntilDone:YES]; | |
[super dealloc]; | |
} |
__block will be retained under ARC - Apple changed this behavior. I could use __weak, but that's more overhead, and I'm not sure what other implications this might would have. (or __unsafe_unretained combined with a CFRetain/CFRelease? feels very hacky...)
Super thanks man.. You saved my a**..
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
__block variables are not retained, so probably this code should work?
Anyway, I should go and read on blocks and GCD.