Monotouch + UIWebView =随机崩溃 [英] Monotouch + UIWebView = Random Crashes

查看:185
本文介绍了Monotouch + UIWebView =随机崩溃的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS 5.0 iPhone和iPad上使用最新的Mono / Monotouch / MonoDevelop的稳定版本。我有一个UIWebView,在模拟器从来不会崩溃,但随机在实际的设备上崩溃的EXC_BAD_ACCESS。基于我用UIWebViews阅读的所有内容,当UIWebView在完成加载之前就被处理的时候很可能发生。

I'm using the latest stable releases of Mono/Monotouch/MonoDevelop on a iOS 5.0 iPhone and iPad. I have a UIWebView that in the emulator never crashes however randomly on the actual devices it crashes on EXC_BAD_ACCESS. Based on everything I've read with UIWebViews that most likely occurs when the UIWebView gets disposed before it finishes loading.

这里是我在ViewDidLoad() :

Here is the code I am using 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 would crash on the actual device randomly, but never in the emulator?

推荐答案

我需要看到崩溃的详细信息,但更多的源代码是100%确定,但我做,因为你的 NSUrlRequest 实例被声明为一个局部变量。 将此变量添加到您的类型的字段中应该可以解决此问题。

I would need to see the crash details and a but more of source code to be 100% certain but I do believe it's caused because your NSUrlRequest instance is declared as a local variable. Promote this variable into a field of your type should solve this.

一旦方法执行完毕,然而,当时它不再被引用,垃圾收集器可以随时收集它。

The instance could still be required once the method is completed it's execution. However at that time it's not referenced anymore and the garbage collector can collect it anytime. If collected then you'll likely get a crash like you mentioned.

在模拟器上没有发生的事实很可能是因为它比设备更快,而且代码可以在GC收集之前完成该实例。 IOW它可能崩溃它只是一个时间的东西,使它大部分时间在模拟器上,几乎从不在设备上。

The fact it does not occur on the simulator is likely caused because it's faster (than the device) and the code can complete before the GC collect that instance. IOW it could crash it's just a timing thing that makes it work most of the time on the simulator and almost never on devices.

这篇关于Monotouch + UIWebView =随机崩溃的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆