user156888
user156888

Reputation:

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

Upvotes: 0

Views: 874

Answers (4)

Philippe Leybaert
Philippe Leybaert

Reputation: 171734

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.

Upvotes: 1

Dave Van den Eynde
Dave Van den Eynde

Reputation: 17405

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?

Upvotes: 0

user156888
user156888

Reputation:

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

Upvotes: 0

Geoff Norton
Geoff Norton

Reputation: 5056

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

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

Upvotes: 2

Related Questions