Magic Studio

专心做有逼格的APP!

asynchronous requests with NSURLConnection: when to release

up vote

0

down vote

favorite


share [fb]

share [tw]

Apple guide is very specific about releasing connection object: it's done in didFailWithError and connectionDidFinishLoading.

Yet, when I do the same, I later get this in zombi-mode

*** -[NSURLConnection releaseDelegate]: message sent to deallocated instance 0x1001045b0

It seems, there's some code in AppKit which releases connection for me.

I'd be happy to assume that Apple guide is wrong, but do not want to get some terrible memory leak or introduce some subtle incompatibility with older OSX versions or something like that.

Is it safe to ignore documentation in this case?

edit
Code creating request

  URLConnectionDelegate *delegate = [[URLConnectionDelegate alloc] initWithSuccessHandler:^(NSData *response) {

      ...

  }];

  [NSURLConnection connectionWithRequest:request delegate:delegate];  


 // I do not release delegate when testing for this issue, not sure whether I should in general

Delegate class itself

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {

  successHandler(receivedData);


  [receivedData release];

  Block_release(successHandler);


 // do we really need this????????

  [connection release];

}

objective-c cocoa nsurlconnection

link|edit

edited Feb 6 at 2:51



asked Feb 6 at 1:24


Nikita Rybak

24.7k32762


78% accept rate



From the docs: ‘NSURLConnection retains its delegate when it is initialized. It releases the delegate when the connection finishes loading, fails, or is canceled.’ It looks like you have over-released the delegate. – Bavarious Feb 6 at 2:29


@Bavarious I don't release delegate here. (whether I should is another question) Error appears when I add [connection release]; and goes away when I remove it. – Nikita Rybak Feb 6 at 2:32


@Bavarious Also, error wording suggests that method releaseDelegate was invoked on released NSURLConnection instance. Not on delegate. – Nikita Rybak Feb 6 at 2:33


Which object is the URL connection delegate? What is its lifecycle? – Bavarious Feb 6 at 2:36


Hmm, maybe. I’m not familiar with zombies. Would you be willing to paste the code that creates the connection and handles its delegate methods? – Bavarious Feb 6 at 2:39

show 4 more comments

Was this post useful to you?     

posted on 2011-12-05 18:03  Mr 布鲁斯  阅读(322)  评论(0编辑  收藏  举报

导航