memory management for dispatch queue &ARC

本文探讨了在不同iOS和macOS版本中使用Grand Central Dispatch时,如何正确管理DispatchQueue的内存。针对ARC环境下,指出了根据不同部署目标版本是否需要手动进行retain和release操作,并提供了预处理器宏来适配不同版本的需求。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

when reading apple's document on "Memory Management For Dispatch Queue", it's content as follows:

Even if you implement a garbage-collected application, you must still retain and release your dispatch queues and other dispatch objects. Grand Central Dispatch does not support the garbage collection model for reclaiming memory.

so is it need if i am using arc? After studying i found that:

If your deployment target is lower than iOS 6.0 or Mac OS X 10. 

You need to use dispatch_retain and dispatch_release on your queue. ARC does not manage them.

If your deployment target is iOS 6.0 or Mac OS X 10.8 or later

ARC will manage your queue for you. You do not need to (and cannot) use dispatch_retain ordispatch_release if ARC is enabled. In fact, if you try to use it, it is a compiler error, just like what happen when you call release in earlier SDK.

To fix it, you may want to remove the dispatch_release, or use preprocessor:

/* header */
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= 60000
    #define NEEDS_DISPATCH_RETAIN_RELEASE 0
#else                                         // iOS 5.X or earlier
    #define NEEDS_DISPATCH_RETAIN_RELEASE 1
#endif

/* implementation */
#if NEEDS_DISPATCH_RETAIN_RELEASE
    dispatch_release(self.reachabilitySerialQueue);
#endif

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值