释放 GCD 调度队列属性的正确方法是什么?
我正在使用一个dispatch_queue,它是通过其所有者的属性访问的,如下所示:
@property (nonatomic, assign) dispatch_queue_t queue;
注意assign
关键字。队列在对象的整个生命周期中使用,因此由对象拥有。当拥有的对象被释放时,我释放队列:
-(void)dealloc
{
dispatch_release(self.queue);
self.queue = nil;
}
如何正确释放它?使用 retain/release
有效吗?
如果在调用释放时队列中有待处理/正在运行的内容,会发生什么情况?
I'm using a dispatch_queue which is accessed through a property of its owner, like this:
@property (nonatomic, assign) dispatch_queue_t queue;
Note the assign
keyword. The queue is used throughout the objects life and thus owned by the object. I release the queue when the owning object is deallocated:
-(void)dealloc
{
dispatch_release(self.queue);
self.queue = nil;
}
How do I properly release this? Would using retain/release
work?
What happens if there is stuff pending/running on the queue while calling release?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
以下内容是从开发者文档中窃取的:
因此,在任何要使用 -retain 的地方都使用dispatch_retain,在任何要使用 -release 的地方都使用dispatch_release。
调度队列遵循与 Objective-C 对象相同的通用内存管理约定。并且在所有排队的块完成之前它们不会被释放。
如果您确实想要一种方法来关闭调度队列:无法通过任何类型的 API 取消所有排队的块,因此它们始终必须运行完成。加快此过程的一种方法是在管理调度队列的类中使用 BOOL 变量:_isValid。当你想关闭队列时,可以将_isValid设置为NO。提交到队列的所有块在执行任何工作之前都应首先检查 _isValid。
旁注:使用 NSOperationQueue 可能更合适。请参阅 Chris Hanson 的博客文章。
The following is stolen from the developer documentation:
So anywhere you would use -retain use dispatch_retain and anywhere you would use -release use dispatch_release.
Dispatch queues follow the same general memory management conventions as objective-c objects. And they won't be dealloc'ed until all blocks queued are finished.
If you do want a way to shut down a dispatch queue: There's no way to cancel all enqueued blocks via any sort of API, so they always must run to completion. One way to expedite this process is to have a BOOL variable in the class managing the dispatch queue: _isValid. When you want to shut down the queue, you can set _isValid to NO. All blocks submitted to the queue should first check _isValid before doing any work.
A side comment: It may be more appropriate to use NSOperationQueue. See Chris Hanson's blog post.
一件有趣的事情是,如果提交到队列的块引用拥有该队列的对象(例如“self”),则该对象将不会命中
dealloc
,直到队列中的所有待处理块都完成反正。这是一个演示这一点的项目:
https://github.com/joshrl/GDCQueueDeallocTest
One interesting thing about this is that if the blocks submitted to the queue reference the object that owns the queue (e.g. "self"), the object will not hit
dealloc
until all pending blocks in the queue are completed anyway.Here is a project that demonstrates this:
https://github.com/joshrl/GDCQueueDeallocTest
这是安全的。挂起/运行队列从系统中保留。调用dispatch_release只会影响队列的保留计数。请参阅dispatch_async 等手册页。
It is safe. pending/running queue is retained from system. Calling dispatch_release just affects the retain count of the queue. See man page of dispatch_async or so forth.
像这样设置你的属性:
Assign 是隐含的,Strong 将保留队列,告诉 ARC 将其视为 NSObject。
setup your property like so:
Assign is implied and Strong will retain the queue telling ARC to treat it as an NSObject.