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 ,如需转载请自行联系原作者


相关文章
|
1月前
|
数据采集 数据安全/隐私保护 Python
【Python】已解决:urllib.error.HTTPError: HTTP Error 403: Forbidden
通过上述方法,可以有效解决 `urllib.error.HTTPError: HTTP Error 403: Forbidden` 错误。具体选择哪种方法取决于服务器对请求的限制。通常情况下,添加用户代理和模拟浏览器请求是最常见且有效的解决方案。
182 10
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
784 0
|
JSON C++ 数据格式
FastAPI(27)- Handling Errors 处理错误 (下)
FastAPI(27)- Handling Errors 处理错误 (下)
617 0
FastAPI(27)- Handling Errors 处理错误 (下)
|
关系型数据库 PostgreSQL Ruby
HTTP 502: Whoops, GitLab is taking too much time to respond.
最近有台云上的服务器需要释放,然后上面跑的 gitlab 也要挪个地方,如在 docker 内运行,gitlab 镜像大约 1.56G,需占用 4G 以上的内存,因资源有限,于是借在其他的服务器上搭建环境(可用内存小于4G),然鹅启动的时候莫名出现 502,Excuse me?接着搜了一些 issue 博客上的解决方案(如修改端口、重启或 hup 某个服务)无果,后来在调整的过程中从日志里发现了一些信息。
12034 0
|
8月前
|
前端开发 JavaScript
Asynchronous, async
Asynchronous(异步)和async(async函数)是JavaScript中用于处理异步操作的关键概念。
61 7
|
开发工具 数据安全/隐私保护
The request you have made requires authentication. (HTTP 401)
The request you have made requires authentication. (HTTP 401)
1433 0
The request you have made requires authentication. (HTTP 401)
|
应用服务中间件 开发工具 nginx
【git】解决:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413
【git】解决:error: RPC failed; HTTP 413 curl 22 The requested URL returned error: 413
621 0
|
开发工具 git
解决 HTTP/2 stream 1 was not closed cleanly before end of the underlying stream
解决 HTTP/2 stream 1 was not closed cleanly before end of the underlying stream
1944 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
|
开发工具 git
git 报错 RPC failed; curl 18 transfer closed with outstanding read data remaining
这个错误是因为项目太久,tag资源文件太大 找了很久网上都说这样: 配置如下: $ git  config --global http.
9776 0