Skip to content

Instantly share code, notes, and snippets.

@steipete
Created November 27, 2011 13:28
Show Gist options
  • Save steipete/1397553 to your computer and use it in GitHub Desktop.
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)
- (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];
}
@shashankpatel
Copy link

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