Monotouch + UIWebView = random crashes

I use the latest stable versions of Mono / Monotouch / MonoDevelop on iOS 5.0 iPhone and iPad. I have a UIWebView that in the emulator never crashes accidentally on real devices that it crashes to EXC_BAD_ACCESS. Based on everything I read using UIWebViews, which most likely occurs when a UIWebView is deleted before the download completes.

Here is the code I use in my ViewDidLoad ():

var urlAddress = BASE_URL + _page; var nsURL = new NSUrl(urlAddress); var nsURLRequest = new NSUrlRequest(nsURL); _webView.Tag = 10; _webView.ScalesPageToFit = true; _webView.AutosizesSubviews = true; _webView.LoadStarted += HandleWebViewLoadStarted; _webView.LoadFinished += HandleWebViewLoadFinished; _webView.LoadRequest(nsURLRequest); this.Add(_webView); 

Any ideas why it accidentally crashed on the device itself, but never in the emulator?

+4
source share
1 answer

I would need to see the details of the failure, but more source code would be 100% sure, but I suppose this is because your NSUrlRequest instance NSUrlRequest declared as a local variable. Promoting this variable in your type field should solve this problem.

An instance may still be required as soon as the method completes its execution. However, at this time it is no longer mentioned, and the garbage collector can collect it at any time. If you collected, you are likely to get a crash, as you mentioned.

The fact that this does not happen on the simulator is most likely due to the fact that it is faster (than the device) and the code may exit before the GC collects this instance. IOW, this can cause it to crash just time, which makes it work most of the time on the simulator and almost never on devices.

+4
source

Source: https://habr.com/ru/post/1388169/


All Articles