How do I avoid capturing self in blocks when implementing an API?

简介:

Short answer

Instead of accessing self directly, you should access it indirectly, from a reference that will not be retained. If you're not using Automatic Reference Counting (ARC), you can do this:

__block MyDataProcessor*dp = self;
self.progressBlock =^(CGFloat percentComplete){[dp.delegate myAPI:dp isProcessingWithProgress:percentComplete];}

The __block keyword marks variables that can be modified inside the block (we're not doing that) but also they are not automatically retained when the block is retained (unless you are using ARC). If you do this, you must be sure that nothing else is going to try to execute the block after the MyDataProcessor instance is released. (Given the structure of your code, that shouldn't be a problem.) Read more about__block.

If you are using ARC, the semantics of __block changes and the reference will be retained, in which case you should declare it __weak instead.

Long answer

Let's say you had code like this:

self.progressBlock =^(CGFloat percentComplete){[self.delegate processingWithProgress:percentComplete];}

The problem here is that self is retaining a reference to the block; meanwhile the block must retain a reference to self in order to fetch its delegate property and send the delegate a method. If everything else in your app releases its reference to this object, its retain count won't be zero (because the block is pointing to it) and the block isn't doing anything wrong (because the object is pointing to it) and so the pair of objects will leak into the heap, occupying memory but forever unreachable without a debugger. Tragic, really.

That case could be easily fixed by doing this instead:

id progressDelegate = self.delegate;
self.progressBlock =^(CGFloat percentComplete){[progressDelegate processingWithProgress:percentComplete];}

In this code, self is retaining the block, the block is retaining the delegate, and there are no cycles (visible from here; the delegate may retain our object but that's out of our hands right now). This code won't risk a leak in the same way, because the value of the delegate property is captured when the block is created, instead of looked up when it executes. A side effect is that, if you change the delegate after this block is created, the block will still send update messages to the old delegate. Whether that is likely to happen or not depends on your application.

Even if you were cool with that behavior, you still can't use that trick in your case:

self.dataProcessor.progress =^(CGFloat percentComplete){[self.delegate myAPI:self isProcessingWithProgress:percentComplete];};

Here you are passing self directly to the delegate in the method call, so you have to get it in there somewhere. If you have control over the definition of the block type, the best thing would be to pass the delegate into the block as a parameter:

self.dataProcessor.progress =^(MyDataProcessor*dp,CGFloat percentComplete){[dp.delegate myAPI:dp isProcessingWithProgress:percentComplete];};

This solution avoids the retain cycle and always calls the current delegate.

If you can't change the block, you could deal with it. The reason a retain cycle is a warning, not an error, is that they don't necessarily spell doom for your application. If MyDataProcessor is able to release the blocks when the operation is complete, before its parent would try to release it, the cycle will be broken and everything will be cleaned up properly. If you could be sure of this, then the right thing to do would be to use a #pragma to suppress the warnings for that block of code. (Or use a per-file compiler flag. But don't disable the warning for the whole project.)

You could also look into using a similar trick above, declaring a reference weak or unretained and using that in the block. For example:

__weak MyDataProcessor*dp = self;// OK for iOS 5 only
__unsafe_unretained MyDataProcessor*dp = self;// OK for iOS 4.x and up
__block MyDataProcessor*dp = self;// OK if you aren't using ARC
self.progressBlock =^(CGFloat percentComplete){[dp.delegate myAPI:dp isProcessingWithProgress:percentComplete];}

All three of the above will give you a reference without retaining the result, though they all behave a little bit differently: __weak will try to zero the reference when the object is released;__unsafe_unretained will leave you with an invalid pointer; __block will actually add another level of indirection and allow you to change the value of the reference from within the block (irrelevant in this case, since dp isn't used anywhere else).

What's best will depend on what code you are able to change and what you cannot. But hopefully this has given you some ideas on how to proceed.

share | edit | flag
 
1  
   
Awesome answer! Thanks, I have a much better understanding about what is going on and how this all works. In this case, I have control over everything so I'll re-architect some of the objects as needed. –  XJones  Oct 21 '11 at 21:18
   
   
Good answer. I've been using __weak myself in an iOS project. It gives you more safety than __block or __unsafe_unretained, but I imagine most people would be calling this in a situation where self is guaranteed to exist. I also like __weak as a keyword because it is more "explicit" about what's being accomplished, whereas the intentions of __block can be ambiguous. –   mikelikespie  Nov 2 '11 at 1:15
7  
   
O_O I was just passing by with a slightly different problem, got stuck reading, and now leave this page feeling all knowledgeable and cool. Thanks! –   Orc JMR  Dec 19 '12 at 8:07
   
   
@OrcJMR You're welcome! –   benzado  Dec 19 '12 at 22:12
   
   
is is correct, that if for some reason on the moment of block execution dp will be released (for example if it was a view controller and it was poped), then line [dp.delegate ... will cause EXC_BADACCESS? –  peetonn  Jan 30 '13 at 1:50
   
add / show 5 more comments

There’s also the option to suppress the warning when you are positive that the cycle will get broken in the future:

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-retain-cycles"

self.progressBlock =^(CGFloat percentComplete){[self.delegate processingWithProgress:percentComplete];}#pragma clang diagnostic pop

That way you don’t have to monkey around with __weakself aliasing and explicit ivar prefixing.

share | edit | flag
 
1  
   
Sounds like a very bad practice that takes more than 3 lines of code that can be replaced with __weak id weakSelf = self; –   Andy  Sep 3 '13 at 14:05 
1  
   
There’s often a larger block of code that can benefit from the suppressed warnings. –   zoul  Sep 3 '13 at 17:56
   
add comment

I believe the solution without ARC also works with ARC, using the __block keyword:

EDIT: Per the Transitioning to ARC Release Notes, an object declared with __block storage is still retained. Use __weak (preferred) or __unsafe_unretained (for backwards compatibility).

// code sample
self.delegate= aDelegate;

self.dataProcessor =[[MyDataProcessor alloc] init];// Use this inside blocks
__block id myself = self;

self.dataProcessor.progress =^(CGFloat percentComplete){[myself.delegate myAPI:myself isProcessingWithProgress:percentComplete];};

self.dataProcessor.completion =^{[myself.delegate myAPIDidFinish:myself];
    myself.dataProcessor = nil;};// start the processor - processing happens asynchronously and the processor is released in the completion block[self.dataProcessor startProcessing];
share | edit | flag
 
   
   
Didn't realize that the __block keyword avoided retaining it's referent. Thanks! I updated my monolithic answer. :-) –   benzado  Oct 21 '11 at 21:41
1  
   
According to Apple docs "In manual reference counting mode, __block id x; has the effect of not retaining x. In ARC mode, __block id x; defaults to retaining x (just like all other values)." –   XJones  Oct 22 '11 at 2:08 
   
   
@XJones Thanks for the clarification, I've edited my answer. –   Tony  Oct 24 '11 at 15:09
   
add comment

For a common solution, I have these define in the precompile header. Avoids capturing and still enables compiler help by avoiding to use id

#defineBlockWeakObject(o) __typeof(o) __weak
#defineBlockWeakSelfBlockWeakObject(self)

Then in code you can do:

BlockWeakSelf weakSelf = self;
self.dataProcessor.completion =^{[weakSelf.delegate myAPIDidFinish:weakSelf];
    weakSelf.dataProcessor = nil;};
欢迎加群互相学习,共同进步。QQ群:iOS: 58099570 | Android: 572064792 | Nodejs:329118122 做人要厚道,转载请注明出处!

















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

相关文章
|
10天前
|
缓存 前端开发 API
API接口封装系列
API(Application Programming Interface)接口封装是将系统内部的功能封装成可复用的程序接口并向外部提供,以便其他系统调用和使用这些功能,通过这种方式实现系统之间的通信和协作。下面将介绍API接口封装的一些关键步骤和注意事项。
|
17天前
|
监控 前端开发 JavaScript
实战篇:商品API接口在跨平台销售中的有效运用与案例解析
随着电子商务的蓬勃发展,企业为了扩大市场覆盖面,经常需要在多个在线平台上展示和销售产品。然而,手工管理多个平台的库存、价格、商品描述等信息既耗时又容易出错。商品API接口在这一背景下显得尤为重要,它能够帮助企业在不同的销售平台之间实现商品信息的高效同步和管理。本文将通过具体的淘宝API接口使用案例,展示如何在跨平台销售中有效利用商品API接口,以及如何通过代码实现数据的统一管理。
|
29天前
|
安全 算法 API
产品经理必备知识——API接口
前言 在古代,我们的传输信息的方式有很多,比如写信、飞鸽传书,以及在战争中使用的烽烟,才有了著名的烽火戏诸侯,但这些方式传输信息的效率终究还是无法满足高速发展的社会需要。如今万物互联的时代,我通过一部手机就可以实现衣食住行的方方面面,比如:在家购物、远程控制家电、自动驾驶等等,背后都离不开我们今天要聊的API接口。
|
29天前
|
数据采集 JSON API
如何实现高效率超简洁的实时数据采集?——Python实战电商数据采集API接口
你是否曾为获取重要数据而感到困扰?是否因为数据封锁而无法获取所需信息?是否因为数据格式混乱而头疼?现在,所有这些问题都可以迎刃而解。让我为大家介绍一款强大的数据采集API接口。
|
1月前
|
安全 API 数据安全/隐私保护
API接口知识小结
应用程序接口API(Application Programming Interface),是提供特定业务输出能力、连接不同系统的一种约定。这里包括外部系统与提供服务的系统(中后台系统)或后台不同系统之间的交互点。包括外部接口、内部接口,内部接口又包括:上层服务与下层服务接口、同级接口。
|
4天前
|
人工智能 API 开发者
免费使用Kimi的API接口,kimi-free-api真香
今年AI应用兴起,各类智能体涌现,但API免费额度有限。为解决这一问题,GitHub上的[kimi-free-api](https://github.com/LLM-Red-Team/kimi-free-api)项目提供了方便,支持高速流式输出、多轮对话等,与ChatGPT接口兼容。此外,还有其他大模型的免费API转换项目,如跃问StepChat、阿里通义Qwen等。该项目可帮助用户免费体验,通过Docker-compose轻松部署。只需获取refresh_token,即可开始使用。这个开源项目促进了AI学习和开发,为探索AI潜力提供了新途径。
173 2
|
9天前
|
JSON 监控 API
在API接口对接中关键示例问题(1)
在API接口对接中,有几个关键的问题需要注意,以确保接口的稳定性、安全性和易用性。以下是这些问题及部分示例代码的简要概述
|
18天前
|
XML API 网络架构
API 常用的接口类型都有哪些?
在软件开发的宏大舞台上,接口充当着不可或缺的角色,确保了不同的软件模块能够高效、无缝地沟通和协作。
|
18天前
|
监控 API 开发者
邮件发送API接口配置步骤?
`邮件发送API让开发者轻松集成邮件功能。选择服务提供商如SendGrid、Mailgun或AWS SES,注册获取API密钥。配置发件人、收件人、主题和内容,调用API发送邮件。处理响应以确认发送成功,并监控性能进行优化。API简化了邮件发送,提升开发效率。`
|
25天前
|
供应链 搜索推荐 BI
深入了解淘宝原数据:获取API接口及其使用场景
在当今数字化的时代,对于电商行业来说,数据具有极大的价值。淘宝作为中国最大的综合电商平台,拥有庞大的商品信息和用户数据。对于开发者和企业来说,淘宝原数据的获取和分析是实现个性化服务和精准营销的基础。本文将介绍如何通过API接口获取淘宝原数据,以及数据的使用场景。

热门文章

最新文章