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

查看:18
本文介绍了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天全站免登陆