UIWebView随机崩溃在:消息发送到解除分配的实例

我有一个随机的UIWebView崩溃使用iOS8.1UIWebView ,使用iPhone5 。 在我的testing中, iOS7上没有出现崩溃

我已经创build了这个github仓库来重现崩溃: https : //github.com/crarau/WebViewCrash

基本上我添加一个UIWebView并加载www.amazon.com

随机应用程序崩溃与EXC_ARM_BREAKPOINT

在控制台上启用僵尸跟踪后 ,会出现以下消息:

 [UIViewAnimationState release]: message sent to deallocated instance 0x14deff70 

这里在UIView负载上发生了什么:

 [super viewDidLoad]; NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.amazon.com"]]; self.webView.layer.cornerRadius = 0; self.webView.userInteractionEnabled = YES; self.webView.multipleTouchEnabled = YES; self.webView.backgroundColor = [UIColor clearColor]; self.webView.scrollView.scrollEnabled = NO; self.webView.scrollView.bounces = NO; [self.webView loadRequest:urlRequest]; 

exception堆栈跟踪:

  Exception Type: EXC_BREAKPOINT (SIGTRAP) Exception Codes: 0x0000000000000001, 0x000000000000defe Triggered by Thread: 2 Thread 0 name: Dispatch queue: com.apple.main-thread Thread 0: 0 libsystem_kernel.dylib 0x2fe054f0 mach_msg_trap + 20 1 libsystem_kernel.dylib 0x2fe052e4 mach_msg + 36 2 CoreFoundation 0x21fed936 __CFRunLoopServiceMachPort + 142 3 CoreFoundation 0x21febedc __CFRunLoopRun + 1012 4 CoreFoundation 0x21f3a20c CFRunLoopRunSpecific + 472 5 CoreFoundation 0x21f3a01e CFRunLoopRunInMode + 102 6 GraphicsServices 0x293330a4 GSEventRunModal + 132 7 UIKit 0x255461cc UIApplicationMain + 1436 8 WebViewCrash 0x0002deec main (main.m:14) 9 libdyld.dylib 0x2fd52aac start + 0 Thread 1 name: Dispatch queue: com.apple.libdispatch-manager Thread 1: 0 libsystem_kernel.dylib 0x2fe052a0 kevent64 + 24 1 libdispatch.dylib 0x00162674 0x154000 + 58996 2 libdispatch.dylib 0x00157496 0x154000 + 13462 Thread 2 name: WebThread Thread 2 Crashed: 0 CoreFoundation 0x2202aea2 ___forwarding___ + 534 1 CoreFoundation 0x21f5cdf4 _CF_forwarding_prep_0 + 20 2 CoreFoundation 0x21f2ee58 CFRelease + 596 3 QuartzCore 0x24f0ba60 CA::release_objects(X::List<void const*>*) + 12 4 QuartzCore 0x24f10dc2 -[CAAnimation dealloc] + 50 5 libobjc.A.dylib 0x2f7ecd5a objc_object::sidetable_release(bool) + 162 6 libobjc.A.dylib 0x2f7ed1a4 (anonymous namespace)::AutoreleasePoolPage::pop(void*) + 400 7 CoreFoundation 0x21fee2a4 __CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION__ + 12 8 CoreFoundation 0x21fede1e __CFRunLoopDoTimer + 646 9 CoreFoundation 0x21fec06e __CFRunLoopRun + 1414 10 CoreFoundation 0x21f3a20c CFRunLoopRunSpecific + 472 11 CoreFoundation 0x21f3a01e CFRunLoopRunInMode + 102 12 WebCore 0x2d362ebe RunWebThread(void*) + 414 13 libsystem_pthread.dylib 0x2fe95e90 _pthread_body + 136 14 libsystem_pthread.dylib 0x2fe95e02 _pthread_start + 114 15 libsystem_pthread.dylib 0x2fe93b8c thread_start + 4 

任何意见表示赞赏谢谢

我在iOS 8上遇到同样的问题,更具体地说,iPad 3的失败次数比其他更多。

根据你的文章和我能find的其他一些文章,这似乎只是iOS8上的UIWebView的一个问题,因为它在iOS7上工作。

我所做的是在WKWebView上使用新的WKWebView ,而不会出现同样的问题,同时继续在iOS7上使用UIWebView

最好的办法是在苹果公司提交一个雷达,同时切换到WKWebView

如果您不使用animation,则可以通过禁用animation来避免此错误。 把它放在你的controller init方法中:

 [UIView setAnimationsEnabled:NO]; 
 [super viewDidLoad]; [[MPVolumeView alloc] init]; <------ add this line, that will be ok too. NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.amazon.com"]]; self.webView.layer.cornerRadius = 0; self.webView.userInteractionEnabled = YES; self.webView.multipleTouchEnabled = YES; self.webView.backgroundColor = [UIColor clearColor]; self.webView.scrollView.scrollEnabled = NO; self.webView.scrollView.bounces = NO; [self.webView loadRequest:urlRequest]; 

这个解决方法似乎解决了我的应用程序。 我对此感到不舒服,但是目前我唯一能够想出来的就是摆脱这次事故。

[self.webView performSelector:@selector(loadRequest 🙂 withObject:urlRequest] afterDelay:0.5];

或者,如果你想要更安全的话,你可以在iOS 8+上使用WKWebView

由于WKWebView不支持本地文件,请启动Web服务器:

 [GCDWebServer setLogLevel:4]; webServer = [[[GCDWebServer alloc] init] retain]; [webServer addGETHandlerForBasePath:@"/" directoryPath:rootDir indexFilename:@"index.html" cacheAge:3600 allowRangeRequests:YES]; [webServer startWithPort:0 bonjourName:nil]; 

然后,根据操作系统版本,创build您的webview:

 if (NSClassFromString(@"WKWebView")) { NSString* jScript = @""; WKUserScript* wkUScript = [[WKUserScript alloc] initWithSource:jScript injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES]; WKUserContentController* wkUController = [[WKUserContentController alloc] init]; [wkUController addUserScript:wkUScript]; WKWebViewConfiguration* wkWebConfig = [[WKWebViewConfiguration alloc] init]; wkWebConfig.userContentController = wkUController; webViewNew = [[WKWebView alloc] initWithFrame:CGRectMake(0.f, 0.f, 1.f, 1.f) configuration:wkWebConfig]; webViewNew.navigationDelegate = self; webViewNew.scrollView.bounces = NO; webViewNew.scrollView.scrollEnabled = NO; webViewNew.backgroundColor = [UIColor redColor]; webViewNew.opaque = YES; } else { webViewOld = [[UIWebView alloc] initWithFrame: CGRectMake (0, 0, 1.0f, 1.0f)]; webViewOld.delegate = self; [[webViewOld scrollView] setBounces: NO]; webViewOld.scrollView.scrollEnabled = NO; webViewOld.scalesPageToFit = YES; [webViewOld setBackgroundColor:[UIColor whiteColor]]; [webViewOld setOpaque:NO]; } 

然后浏览到:[NSString stringWithFormat:“htztp://127.0.0.1:%d”,(int)webServer.port](忽略http中的z,stackoverflow不会让我张贴URL到本地主机)

我解决了这个问题。 我有同样的问题添加一个video标签(源是一个本地mp4文件约18Mb)。 我可以将video标签识别为崩溃,因为没有video的html不会崩溃。 然后我试图注入两秒钟的video标签通过JavaScript和应用程序崩溃。 几个小时后,我发现这个: UIWebView加载html5-Video EXC_BAD_ACCESS崩溃 。

我的解决scheme是在JavaScript函数中提取video标记注入,并在webViewDidFinishLoad上调用该函数。

所以,在我的控制器中我有:

 - (void)webViewDidFinishLoad:(UIWebView *)webView { [self.webView stringByEvaluatingJavaScriptFromString:@"placeVideoTag();"]; } 

而在我的index.html我有:

 ... <div id="left"> <div id="media-div"></div> ... 

和(我也添加了audio标签!):

 <script> function placeVideoTag() { $('#media-div').html('<video id="videoController" width="100%" controls > <source src="video_1.mp4" type="video/mp4"></video>'); $('#media-div').append('<br/><audio id="audioController" controls="" width="100%"><source src="audio_1.mp3" type="audio/mp3"></audio>'); } </script> 

我知道这听起来是一个奇怪的解决scheme,但它确实工作。 我也遇到了同样的问题,也是僵尸追踪

随机应用程序崩溃与EXC_ARM_BREAKPOINT的WebThread

在控制台上启用僵尸跟踪后,会出现以下消息:

[UIViewAnimationState发布]:将消息发送到解除分配的实例0x14deff70

确保在dealloc方法中将UIWebView委托设置为nil

 - (void)dealloc { [self.webView stopLoading]; self.webView.delegate = nil; } 

尝试更新到iOS 8.4.1。 它为我们工作(但是,我们在发行说明中没有find任何对此错误的引用)。

我知道迟到了,但这可以帮助别人,确保你的IBOutlet的意见是薄弱的。 这个答案帮助我摆脱了这个问题。