gpt4 book ai didi

ios - 为什么 ARC 提示 iOS 6 中的 dispatch_queue_create 和 dispatch_release?

转载 作者:IT王子 更新时间:2023-10-29 08:07:48 26 4
gpt4 key购买 nike

我声明了一个引用 GCD 队列的属性:

@property (assign) dispatch_queue_t backgroundQueue;

在类的 init 方法中我创建了一个串行队列:

backgroundQueue = dispatch_queue_create("com.company.app", DISPATCH_QUEUE_SERIAL);

ARC 提示:“将保留的对象分配给 unsafe_unretained 变量;对象将在分配后释放”

我必须使用 __bridge_transfer 吗?

在 -dealloc 中我正在释放队列:

dispatch_release(backgroundQueue);

再次,ARC 提示:“ARC 禁止显式发送‘release’消息”

我觉得这很困惑,因为这是一个 C 函数调用,并且认为队列是 C 对象,我必须自己负责内存管理! ARC 什么时候开始为我处理 C 对象?

最佳答案

在 iOS 6 中,您可以 cmd+click dispatch_queue_t 并看到这个:

/*
* By default, dispatch objects are declared as Objective-C types when building
* with an Objective-C compiler. This allows them to participate in ARC, in RR
* management by the Blocks runtime and in leaks checking by the static
* analyzer, and enables them to be added to Cocoa collections.
* See <os/object.h> for details.
*/

因此只需在属性中使用 strong(除非队列在其他地方被引用并且您确实需要弱引用)。

在 iOS 6 之前,您必须使用 dispatch_retain 和 dispatch_release 自己进行内存管理。在 iOS 6 中这样做会引发编译器错误。

关于ios - 为什么 ARC 提示 iOS 6 中的 dispatch_queue_create 和 dispatch_release?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13702701/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com