Re: Crash from NSURLConnection if delegate released too soon?
Re: Crash from NSURLConnection if delegate released too soon?
- Subject: Re: Crash from NSURLConnection if delegate released too soon?
- From: Howard Moon <email@hidden>
- Date: Mon, 25 Aug 2014 13:43:55 -0700
Damn. That didn't fix it. Several tests since then have seen it happen again. I changed the connection to use this request, but it didn't change anything:
NSMutableURLRequest *postRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"https://transactions.antarestech.com/securevocal/ProductVersionProvider.aspx"]
cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData
timeoutInterval:10.0];
I don't see what the problem is. I've tried it exactly like the Apple reference documents show, and I've got it slightly changed from that, as shown here:
http://cagt.bu.edu/w/images/8/8b/URL_Connection_example.txt
but no luck either way.
The Apple docs show simply setting the connection and receivedData to nil, instead of calling release on them. The docs don't show the declaration of dataReceived (or if there is a @property in the .h and @syntheize in the .m (or .mm) file), as the cagt example does. The cagt example also shows retaining dataReceived if the connection is created:
self.receivedData = [[NSMutableData data] retain];
This would be why it's released in those functions instead of simply set to nil as the Apple docs show. But I've tried it both ways, and no difference.
I've also tried releasing the connection itself immediately after creation, as some online posts have stated. This does not help, either.
I do notice one error in the Apple docs: they show "theConnection" as a local variable declared in the function where the connection is created, but then release "theConnection" in both connectionDidFinishLoading and didFailWithError, even though the parameter for both of those functions is declared as "connection", not "theConnection". This means that their code will not compile (and is what initially led me to declare theConnection as a member of my delegate class). If it won't even compile, it's no surprise it doesn't work as expected, either.
Is there an example of code out there that actually *works*? I'm doing a one-time POST upon opening my plug-in's editor window (assuming it has not already been done by this instance or by another instance of the plug-in). It should be simple enough to just follow the guidelines and Apple's example, but I've spent a few days trying to get this stupid thing to work!
Thanks,
Howard
On Aug 25, 2014, at 9:54 AM, Jens Alfke <email@hidden> wrote:
>
>> On Aug 25, 2014, at 9:40 AM, Howard Moon <email@hidden> wrote:
>>
>> Ok, I fixed it. I was using the connection object as a member of my class, and releasing that, when I should have been releasing the connection that is given to me inside didFailWithError and connectionDidFinishLoading, not making it a member at all.
>
> It should be OK either way as long as you're following the ref-counting rules (or are using ARC.)
> * Only release the NSURLConnection if you allocated it by calling +alloc
> * Don't release the NSURLConnection instance passed into your delegate methods
>
> Also, NSURLConnection instances always stay alive until the connection finishes (they are retained by CFNetwork internally) so you actually don't need to retain them yourself.
>
> —Jens
_______________________________________________
Cocoa-dev mailing list (email@hidden)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden