GCD 捕获 self 是否会形成内存泄漏?

背景

关于 GCD 的 block 捕获 self 是否形成循环引用的问题,网上是争论不休,在 iOS 的面试中更是频繁出现。咱们从 YYKit 里面的一个 Issue 出发,来探索一下 GCD 跟 self 之间是否会形成循环引用的问题。git

该 Issue 起源于 YYKit 中的一段代码:github

- (void)_trimInBackground {
    __weak typeof(self) _self = self;
    dispatch_async(_queue, ^{
        __strong typeof(_self) self = _self;
        /* 此处省略一万字 **/
    });
}
复制代码

能够看到,YY 大神在 GCD 中,为了不循环引用,使用了 strong-weak dance,可是网友在该 Issue 中提出,苹果的 dispatch_async 函数在 block 任务执行完成后会将该 block 进行 Block_release,并不会形成循环引用,此处用 strong-weak dance 反而可能形成 block 执行前 self 就已经被释放。面试

而 YY 大神的观点则认为,因为self 持有一个 _queue 变量,而 _queue 会持有该 block,此时在 block 内直接捕获 self 则会形成循环引用。(self->_queue->block->self)api

然而这样真的会形成内存泄漏吗?async

探索

咱们建立一个简单的 Demo,代码以下:函数

@interface ViewController2 ()

@property (nonatomic, strong) dispatch_queue_t queue;

@end


@implementation ViewController2

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.queue = dispatch_queue_create("com.mayc.concurrent", DISPATCH_QUEUE_CONCURRENT);
    dispatch_async(self.queue, ^{
        [self test];
    });
}

- (void)test {
    NSLog(@"test");
}

- (void)dealloc {
    NSLog(@"dealloc");
}

@end

复制代码

在 Demo 里,ViewController2 持有一个 queue 变量,dispatch_async 的 block 中捕获了 self。咱们打开一个 ViewController2 的页面,而后关掉;若是 dispatch_async 强捕获 self 会形成内存泄漏,那么 ViewController2 的 dealloc 方法必然是不会执行的。 执行结果以下:源码分析

2020-03-11 15:36:35.352789+0800 MCDemo[83661:22062265] test
2020-03-11 15:36:36.922477+0800 MCDemo[83661:22062108] dealloc
复制代码

能够看到 ViewController2 被正常释放了,也就是说并不会形成内存泄漏ui

源码分析

源码面前无秘密,咱们看一下 dispatch_async 的源码:this

void dispatch_async(dispatch_queue_t dq, dispatch_block_t work) {
	dispatch_continuation_t dc = _dispatch_continuation_alloc();
	uintptr_t dc_flags = DC_FLAG_CONSUME;
	dispatch_qos_t qos;
    // 将work(也就是咱们传进来的任务 block)封装成 dispatch_continuation_t
	qos = _dispatch_continuation_init(dc, dq, work, 0, dc_flags);
	_dispatch_continuation_async(dq, dc, qos, dc->dc_flags);
}
复制代码

能够看到,dispatch_async 传入的 block 最终会与其余参数封装成 dispatch_continuation_t,咱们重点看一下这块封装的代码(如下代码有所精简):atom

static inline dispatch_qos_t
_dispatch_continuation_init(dispatch_continuation_t dc,
		dispatch_queue_class_t dqu, dispatch_block_t work,
		dispatch_block_flags_t flags, uintptr_t dc_flags)
{
    // 拷贝block
	void *ctxt = _dispatch_Block_copy(work);

	dc_flags |= DC_FLAG_BLOCK | DC_FLAG_ALLOCATED;

	dispatch_function_t func = _dispatch_Block_invoke(work);
	if (dc_flags & DC_FLAG_CONSUME) {
        // 顾名思义,执行block,而后释放。
		func = _dispatch_call_block_and_release;
	}
	return _dispatch_continuation_init_f(dc, dqu, ctxt, func, flags, dc_flags);
}

// _dispatch_call_block_and_release的源码以下
void _dispatch_call_block_and_release(void *block)
{
	void (^b)(void) = block;
	b();	// 执行
	Block_release(b);	// 释放
}
复制代码

能够看到正如 Apple 文档所说,dispatch_async 会在 block 执行完成后将其释放。所以 _self->queue->block->self 这个循环引用只是暂时的(block 执行完成后被释放,打断了循环引用)。

刨根问底

dispatch_sync

既然 dispatch_async 的 block 捕获 self 不会形成循环引用,那么换成 dispatch_sync 会怎么样呢?

self.queue = dispatch_queue_create("com.mayc.concurrent", DISPATCH_QUEUE_CONCURRENT);
 dispatch_sync(self.queue, ^{
     [self test];
 });
复制代码

其实 dispatch_sync 也不会有问题。咱们把刚刚 Demo 中的 dispatch_async 换成 dispatch_sync ,能够看到也未形成内存泄漏。

2020-03-11 17:05:18.840834+0800 MCDemo[5437:69508] test
2020-03-11 17:05:20.419588+0800 MCDemo[5437:68626] dealloc
复制代码

不过 dispatch_sync 不会形成 _self->queue->block->self 循环引用的缘由跟 dispatch_async 有所不一样,不是由于执行完成后被 release,咱们看一下官方关于 dispatch_sync 的文档有段说明:

Unlike with dispatch_async, no retain is performed on the target queue. Because calls to this function are synchronous, it "borrows" the reference of the caller. Moreover, no Block_copy is performed on the block.

大体意思是说,queue 不会对 block 进行持有,也不会进行 Block_copy 操做。既然 queue -> block 这一层引用不存在,天然也不会形成循环引用

dispatch_after 等其余 GCD api

咱们在 dispatch_after、dispatch_group_async 的官方文档里面也看能够到和 dispatch_async 相似的话:

_This function performs a Block_copy and Block_release on behalf of the caller. _

能够看到这些 GCD api 的方法也都作了 release 处理,所以其余的这些也不会由于捕获 self 而形成循环引用。

拓展

既然就算 self 持有 queue 也不会形成 GCD 的循环引用,那若是是 self 直接持有 GCD 的 block 呢?

dispatch_queue_t queue = dispatch_queue_create("com.mayc.concurrent", DISPATCH_QUEUE_CONCURRENT);
self.block = ^{ 
    [self test]; 
};
dispatch_async(queue, self.block);
复制代码

emm...若是非要这样的话,确定是会内存泄漏的....这是由于 block 被 self 直接持有,同时在 gcd 中进行了一次 Block_copy 操做,引用计数器为 2。block 任务执行完成后进行 Block_release,此时引用计数器为1 ,这种状况下 block 不会被清理。

相关文章
相关标签/搜索