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


相关文章
|
9月前
|
安全 API 数据安全/隐私保护
Django REST framework安全实践:轻松实现认证、权限与限流功能
Django REST framework安全实践:轻松实现认证、权限与限流功能
|
9月前
|
存储 关系型数据库 MySQL
零基础学java---private和this关键字(2)
零基础学java---private和this关键字(2)
87 0
零基础学java---private和this关键字(2)
|
开发者 索引 Python
Python常用数据结构和运算|学习笔记
快速学习Python常用数据结构和运算
|
存储 XML 数据格式
XML的验证模式
XML文件的验证模式保证了XML文件的正确性,而比较常用的验证模式有两种:DTD和XSD。 DTD与XSD区别 DTD(Document Type Definition)即文档类型定义,是一种XML约束模式语言,是XML文件的验证机制,属于XML文件组成的一部分。
812 0
【1003】Emergency (25 分)
【1003】Emergency (25 分) 【1003】Emergency (25 分)
127 0
|
测试技术 C++ Windows
带你读《2022技术人的百宝黑皮书》——跨桌面端之组件化实践(7)
带你读《2022技术人的百宝黑皮书》——跨桌面端之组件化实践(7)
|
8月前
|
存储 开发工具 Android开发
代码协同模式使用问题之创建特性分支,如何解决
代码协同模式使用问题之创建特性分支,如何解决
|
算法 索引 机器学习/深度学习