在类方法中使用中央调度会导致内存泄漏
当视图控制器在创建 gcd 队列的行调用模型类方法时,出现内存泄漏。有什么想法吗?
+(void)myClassMethod {
dispatch_queue_t myQueue = dispatch_queue_create("com.mysite.page", 0); //run with leak instrument points here as culprit
dispatch_async(myQueue, ^{});
}
I get a memory leak when the view controller calls my model class method at the line where i create my gcd queue. Any ideas?
+(void)myClassMethod {
dispatch_queue_t myQueue = dispatch_queue_create("com.mysite.page", 0); //run with leak instrument points here as culprit
dispatch_async(myQueue, ^{});
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该将其更改为……
当您不再需要访问队列时,您应该调用
dispatch_release
。由于myQueue
是局部变量,因此您必须在那里调用它。阅读dispatch_queue_create文档:
讨论
提交到队列的块按照先进先出的顺序一次执行一个。但请注意,提交到独立队列的块可以相对于彼此同时执行。
当您的应用程序不再需要调度队列时,应该使用dispatch_release函数释放它。提交到队列的任何待处理块都保留对该队列的引用,因此在所有待处理块完成之前不会释放队列。
You should change it to ...
... you should call
dispatch_release
when you no longer need an access to the queue. And asmyQueue
is local variable, you must call it there.Read dispatch_queue_create documentation:
Discussion
Blocks submitted to the queue are executed one at a time in FIFO order. Note, however, that blocks submitted to independent queues may be executed concurrently with respect to each other.
When your application no longer needs the dispatch queue, it should release it with the dispatch_release function. Any pending blocks submitted to a queue hold a reference to that queue, so the queue is not deallocated until all pending blocks have completed.
泄漏工具报告内存分配的位置,不再有任何来自代码的引用。
该方法运行后,由于没有任何内容引用您创建的队列,并且从未调用过dispatch_release(),因此它被视为泄漏。
The Leak tool reports where memory is allocated that no longer has any references from your code.
After that method runs, since there is nothing that has a reference to the queue you created, and dispatch_release() was never called, it's considered a leak.