开发者

UiWebView crashes iPhone

The following view crashes the iPhone

If I comment out the

using (webView = new UIWebView(webRect)) {
}

section - it does not.

using System;
using MonoTouch.UIKit;
using MonoTouch.Foundation;
using System.Drawing;

namespace WmcStarClient
{


    public class EventDetailView : UIViewController
    {
        public WmcStarEventService.Event _event;
        public UIWebView webView;

        public EventDetailView ()
        {
        }

        public EventDetailView (WmcStarEventService.Event eventParam)
        {
            _event = eventParam;
        }

        public override void ViewDidLoad ()
       开发者_运维百科 {
            base.ViewDidLoad ();

            var webRect = new RectangleF(0f, 0f, 320f, 460f);

            using (webView = new UIWebView(webRect)) {
            }
        }
    }
}

has anyone seen this before?

w://


This was a bug that was fixed in the first beta of the 1.5 series:

http://monotouch.net/Releases/MonoTouch_1.4.99


When you're using the "using" construct, you are calling Dispose() on the webview, which is mapped to the "release" message in the Cocoa runtime.

So you are releasing the webview object, but you're still holding on to the reference in your class. The first time you try to access it, you're accessing a released instance.

Aren't you adding the webview to your view as a subview? If so, at what point?

The correct code, IMHO, would be:

    public class EventDetailView : UIViewController
    {
        public WmcStarEventService.Event _event;
        public UIWebView webView;

        public EventDetailView ()
        {
        }

        public EventDetailView (WmcStarEventService.Event eventParam)
        {
            _event = eventParam;
        }

        public override void ViewDidLoad ()
        {
            base.ViewDidLoad ();

            var webRect = new RectangleF(0f, 0f, 320f, 460f);

            webView = new UIWebView(webRect);

            View.AddSubView(webView);
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing)
               webView.Dispose();

            base.Dispose(disposing);
        }
    }

Note that overriding Dispose() is not required, but it should help to release memory earlier than simply relying on the garbage collector.


This is potentialy a different/new bug - i've gotten a better response over at the monotouch forums:

http://forums.monotouch.net/yaf_postsm1500.aspx#post1500

w://


It seems very strange to me to wrap that in a using statement. It's as if your disposing something while still loading. Are you sure that's right?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜