How do I avoid capturing self in blocks when implementing an API?
Short answerInstead of accessing
The If you are using ARC, the semantics of Long answerLet's say you had code like this:
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:
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:
Here you are passing
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 You could also look into using a similar trick above, declaring a reference weak or unretained and using that in the block. For example:
All three of the above will give you a reference without retaining the result, though they all behave a little bit differently: 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. |
|||||||||||||||||||||||||||||||||
|


|
There’s also the option to suppress the warning when you are positive that the cycle will get broken in the future:
That way you don’t have to monkey around with |
|||||||||||||||
|
|
I believe the solution without ARC also works with ARC, using the EDIT: Per the Transitioning to ARC Release Notes, an object declared with
|
|||||||||||||||||||||
|
|
For a common solution, I have these define in the precompile header. Avoids capturing and still enables compiler help by avoiding to use
Then in code you can do:
|
How do I avoid capturing self in blocks when implementing an API?的更多相关文章
- Some good iOS questions
这里,我列举了一些在Stackoverflow中一些比较好的关于iOS的问题.大部分我列举的问题都是关于Objective C.所有问题中,我比较喜欢“为什么”这一类型的问题. 问题 1. What’ ...
- [转]JavaScriptCore and iOS 7
原文:http://www.bignerdranch.com/blog/javascriptcore-and-ios-7/ As a rule, iOS programmers don't think ...
- [转]UIWebView的Javascript运行时对象
An alternative, that may get you rejected from the app store, is to use WebScriptObject. These APIs ...
- 【Orleans开胃菜系列2】连接Connect源码简易分析
[Orleans开胃菜系列2]连接Connect源码简易分析 /** * prism.js Github theme based on GitHub's theme. * @author Sam Cl ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- x264源代码简单分析:滤波(Filter)部分
===================================================== H.264源代码分析文章列表: [编码 - x264] x264源代码简单分析:概述 x26 ...
- Async Performance: Understanding the Costs of Async and Await
Stephen Toub Download the Code Sample Asynchronous programming has long been the realm of only the m ...
- .NET Best Practices
Before starting with best practices tobe followed, it is good to have clear understanding of how mem ...
- Architecture of a Java Compiler
Architectural Overview A modern optimizing compiler can be logically divided into four parts: Th ...
随机推荐
- 【BZOJ】2743: [HEOI2012]采花(树状数组)
题目 传送门:QWQ 分析 已经凉凉.看错数据范围敲了发莫队........ 和HH的项链差不多,把每种颜色之前的颜色到再之前的颜色这段区间 区间加. 区间加就树状数组特技 代码 #include & ...
- IIS Worker Process 遇到了一个问题,需要关闭
服务器为2003系统,平时都用的好好的,但是最近经常跳出了!IIS Worker Process 遇到问题关闭! 第二个对话框还有个请单击此处的连接 以下文件将包含在这个错误报告中:C:\DOCUME ...
- 对Node的优点和缺点提出了自己的看法?
(优点)因为Node是基于事件驱动和无阻塞的,所以非常适合处理并发请求, 因此构建在Node上的代理服务器相比其他技术实现(如Ruby)的服务器表现要好得多. 此外,与Node代理服务器交互的客户端代 ...
- PCI、PCI-x,PCI-E兼容以及他们之间的区别详细图解
一.PCI PCI接口分为32bit和64bit两种,32bit就是一般台式机使用的普通的pci接口(图一.图三),64bit接口比32bit接口长一些一般只出现在服务器上(图四.图五).32bit和 ...
- Springmvc ajax请求400
转载做记录 传JSON对象 前端 function test () { var param = {username : "yitop"}; $.ajax({ timeout : 2 ...
- c++获取lua嵌套table某属性的值
开发环境:vs2012 lua版本:LuaJIT-2.0.2 lua文件作为配置文件,c++读取这个配置. lua配置结构如下 SceneConfig = { [] = { name =}, [] = ...
- Tkinter简易教程
支持python的常见GUI工具包: Tkinter 使用Tk平台 很容易得到 半标准 wxpython 基于wxWindows.跨平台越来越流行 Python Win 只能在Windows上使用 使 ...
- python变量、引用、拷贝之间的关系
Python中一切皆为对象,不管是集合变量还是数值型or字符串型的变量都是一个引用,都指向对应内存空间中的对象. 简而言之: 变量直接赋值:新变量本身及其内部的元素都与原变量指向相同的内存空间,并且值 ...
- 03-24 Winform图表Chart
图表主要分为以下几个部分: 1.Annotations--图形注解集合, 2.ChartAreas--图表区域集合, 3.Legends--图例集合, 4.Series--图表序列集合(即图表数据对象 ...
- Spring技术内幕之Spring Data JPA-自定义Repository实现
1.自定义Repository方法接口,让接口的实现类来继承这个中间接口而不是Repository接口 package com.data.jpa.dao; import java.io.Seriali ...