在类方法中使用中央调度会导致内存泄漏

发布于 2024-10-26 16:29:40 字数 259 浏览 0 评论 0原文

当视图控制器在创建 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

花间憩 2024-11-02 16:29:40

您应该将其更改为……

dispatch_queue_t myQueue = dispatch_queue_create("com.mysite.page", 0);
dispatch_async(myQueue, ^{});
dispatch_release(myQueue);

当您不再需要访问队列时,您应该调用dispatch_release。由于 myQueue 是局部变量,因此您必须在那里调用它。

阅读dispatch_queue_create文档:

讨论

提交到队列的块按照先进先出的顺序一次执行一个。但请注意,提交到独立队列的块可以相对于彼此同时执行。

当您的应用程序不再需要调度队列时,应该使用dispatch_release函数释放它。提交到队列的任何待处理块都保留对该队列的引用,因此在所有待处理块完成之前不会释放队列。

You should change it to ...

dispatch_queue_t myQueue = dispatch_queue_create("com.mysite.page", 0);
dispatch_async(myQueue, ^{});
dispatch_release(myQueue);

... you should call dispatch_release when you no longer need an access to the queue. And as myQueue 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.

围归者 2024-11-02 16:29:40

泄漏工具报告内存分配的位置,不再有任何来自代码的引用。

该方法运行后,由于没有任何内容引用您创建的队列,并且从未调用过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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文