Hi again.

I have implemented NSUrlProtocol to trap http requests and handle them.

It works well but some times i'm getting random exceptions about the client
being null, Url being null and once got a disposed exception.

It seems like sometimes the protocol is being disposed by the garbage
collector but I have a reference to the protocol handler and never dispose
it.

Any idea on how to stop this??

Here is the code of my protocol handler and the instantiation of it:


        public class ProtocolHandler : NSUrlProtocol
        {
                private NSUrlConnection Connection;

                private NSMutableUrlRequest req;

                public override NSUrlRequest Request 
                {
                        get 
                        {
                                return req;
                        }
                }

                public ProtocolHandler ()
                {
                }

                [Export ("initWithRequest:cachedResponse:client:")]
                public ProtocolHandler (NSUrlRequest Request, 
NSCachedUrlResponse
CachedResponse, NSUrlProtocolClient Client) : base(Request, CachedResponse,
Client)
            {
                        req = (NSMutableUrlRequest)Request.MutableCopy();
                        req["X-Protocol-gDocs"] = "Yes";
            }       

                [Export ("canInitWithRequest:")]
                public static new bool CanInitWithRequest (NSUrlRequest request)
                {
                        try {
                                if (request == null || request.Url == null)
                                        return false;

                                if ((request.Url.Scheme == "http" || 
request.Url.Scheme == "https") &&
(request.Headers == null || request.Headers ["X-Protocol-gDocs"] == null))
//<-- random crash, Url is null.  ¿?
                                        return true;

                                return false;
                        } catch {
                                return false;
                        }
            }

                [Export ("canonicalRequestForRequest:")]
                public static new NSUrlRequest GetCanonicalRequest (NSUrlRequest
forRequest)
                {
                        return forRequest;
                }

                public override void StartLoading ()
                {
                        ProtocolConnectionDelegate del = new 
ProtocolConnectionDelegate();
                        del.Handler = this;
                        this.Connection = new NSUrlConnection(this.Request, 
del);
                }

                public override void StopLoading ()
                {
                        try {
                                this.Connection.Cancel ();
                                this.Connection.Dispose ();
                                this.Connection = null;
                        } catch {
                        }
                }
        }


----

and on my AppDelegate:

---

                Class currentHandler;

                public override bool FinishedLaunching (UIApplication app,
NSDictionary options)
                {

                        //...

                        currentHandler = new Class(typeof(ProtocolHandler));

                        NSUrlProtocol.RegisterClass(currentHandler);

                }



--
View this message in context: 
http://monotouch.2284126.n4.nabble.com/Random-crashes-using-NSUrlProtocol-tp4657239.html
Sent from the MonoTouch mailing list archive at Nabble.com.
_______________________________________________
MonoTouch mailing list
[email protected]
http://lists.ximian.com/mailman/listinfo/monotouch

Reply via email to