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?     

欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!












本文转自张昺华-sky博客园博客,原文链接:http://www.cnblogs.com/sunshine-anycall/archive/2011/12/05/2277119.html ,如需转载请自行联系原作者


相关文章
getReader() has already been called for this request
getReader() has already been called for this request
1734 0
getReader() has already been called for this request
|
8月前
|
XML JSON 前端开发
Asynchronous
【7月更文挑战第6天】
68 5
Flutter之运行提示Could not update files on device: Connection closed before full header was received
Flutter之运行提示Could not update files on device: Connection closed before full header was received
803 0
|
10月前
|
前端开发 JavaScript
Asynchronous, async
Asynchronous(异步)和async(async函数)是JavaScript中用于处理异步操作的关键概念。
69 7
|
前端开发
The following tasks did not complete: first Did you forget to signal async completion?
The following tasks did not complete: first Did you forget to signal async completion?
|
Java 关系型数据库 MySQL
10. 成功解决:io.netty.channel.ChannelPipelineException: ***Handler is not a @Sharable handler, so can't be added or removed multiple times.
使用 SpringBoot 集成 Netty 时,报如下错误:io.netty.channel.ChannelPipelineException: ***Handler is not a @Sharable handler, so can't be added or removed multiple times.
702 0
|
网络协议 安全 Unix
Coroutine made DPDK development easy
This is a English translation of the article 《有了协程库,开发DPDK应用程序第一次可以这么简单》. The original link is https://developer.aliyun.com/article/1208390
Gradle sync failed: Unsupported method: NativeArtifact.getSourceFolders().
Gradle sync failed: Unsupported method: NativeArtifact.getSourceFolders().
117 0
|
Dart 开发工具 Android开发
【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
590 0
【错误记录】Flutter 报错 Downloading the Dart SDK using the BITS service failed, retrying with WebRequest...
|
存储 安全 前端开发
These are some of the existing methods to implement IPC on iOS
These are some of the existing methods to implement IPC on iOS
201 0
These are some of the existing methods to implement IPC on iOS