21

I'm having a random UIWebView crash using iOS8.1 and UIWebView, using an iPhone5. In my tests the crash doesn't appear on iOS7.

I've created this github repository to reproduce the crash: https://github.com/crarau/WebViewCrash

Basically I'm adding a UIWebView and loading www.amazon.com

Randomly the app is crashing on the WebThread with EXC_ARM_BREAKPOINT

After enabling Zombie tracking on the console there is this message:

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

Here what happens on UIView load:

[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];

The exception stacktrace:

    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

Any advice is appreciated Thanks

Dharmesh Kheni
  • 71,228
  • 33
  • 160
  • 165
Ciprian Rarau
  • 3,040
  • 1
  • 30
  • 27

8 Answers8

7

I'm experiencing the same issue on iOS 8 and more specifically an iPad 3 fails much more often than others.

Based on your post and a couple of others that I was able to find, it seems that it's an issue with UIWebView on iOS8 only, as it works on iOS7.

What I've done is to use the new WKWebView on iOS8, which doesn't present the same issue, while continuing to use UIWebView on iOS7.

The best thing to do would be to file a radar with Apple and switch to WKWebView in the meantime.

Andrew
  • 3,166
  • 21
  • 32
  • I didn't found how to set "scalesPageToFit" property for WKWebView – Roman Truba Nov 06 '14 at 15:17
  • 1
    It seems that it's a bit more involved with WKWebView - http://stackoverflow.com/questions/26295277/wkwebview-equivalent-for-uiwebviews-scalespagetofit – Andrew Nov 06 '14 at 15:20
  • Thank you. I've replaced a UIWebView to a WKWebView and crashes disappeared. I got a couple little bugs with youtube player, but they are slight near system crashes. – Roman Truba Dec 17 '14 at 09:17
  • 1
    As of 2015-01-30 the big issue with WKWebView is that NSURLProtocol and NSURLSession do not work, so it's impossible to intercept and modify requests, which makes it a bad canditate for replacing UIWebView. – klaus triendl Jan 30 '15 at 09:25
6

If you are not using animations you can avoid this bug by disabling Animations. Put this in your controller init method:

[UIView setAnimationsEnabled:NO];
Cjolly
  • 482
  • 5
  • 7
  • This worked perfectly for what I needed. Saved me having to write a load of code to incorporate WKWebView. Many thanks. – pls Mar 26 '15 at 17:49
  • 1
    I was getting a crash from a message being sent to a deallocated `UIViewAnimationState` about 1 in 5 times over about 50 launches when launching my cordova iOS app. Following this advice I just launched it 30 straight times with no error. – jandersen May 01 '15 at 06:30
  • this helped me with my spritekit game – user2995344 May 27 '15 at 00:58
2
[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];
shuice
  • 49
  • 4
1

This work around appears to fix it for my app. I'm not comfortable with this, but it's currently the only thing I've been able to figure out to get rid of the crash.

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

Or, if you want something safer you can use WKWebView on iOS 8+

Since WKWebView doesn't support local files, start a web server:

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

Then, depending on the OS version, create your 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];
}

And then browse to: [NSString stringWithFormat: "htztp://127.0.0.1:%d", (int)webServer.port] (Ignore the z in the http, stackoverflow won't let me post urls to localhost)

Roland Rabien
  • 8,750
  • 7
  • 50
  • 67
0

I solved the problem. I had the same issue adding a video tag (source was a local mp4 file approx. 18Mb). I could identify the video tag as the crash responsible because html without video doesn't crash. Then i tried to inject two seconds later the video tag via javascript and the app crashed. After a few hours i found this: UIWebView loading html5-Video EXC_BAD_ACCESS crash.

My solution was to extract the video tag injection in a javascript function and call that function on webViewDidFinishLoad.

So, in my controller i have:

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

While in my index.html i have:

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

and (i added an audio tag too!):

<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>

I know it sounds as a strange solution, but it is really working. I had the same identical issue as stated here, also enabling Zombie tracking

Randomly the app is crashing on the WebThread with EXC_ARM_BREAKPOINT

After enabling Zombie tracking on the console there is this message:

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

Community
  • 1
  • 1
Giorgio Andretti
  • 198
  • 1
  • 15
0

make sure you set the UIWebView delegate to nil in the dealloc method

- (void)dealloc {
  [self.webView stopLoading];
   self.webView.delegate = nil;
}
Tomer Even
  • 4,820
  • 2
  • 30
  • 36
0

Try to update to iOS 8.4.1. It worked for us (however, we didn't find any references to this bug in release notes).

abstractdog
  • 256
  • 4
  • 4
-1

I know am late but this could help someone, make sure your IBOutlet views are weak. This answer helped me to get rid of this issue.

Community
  • 1
  • 1
user3894518
  • 677
  • 5
  • 10