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

时间:2021-12-08 23:43:30

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?

当视图控制器在我创建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, ^{});
}

2 个解决方案

#1


18  

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.

...当您不再需要访问队列时,应该调用dispatch_release。因为myQueue是局部变量,所以你必须在那里调用它。

Read dispatch_queue_create documentation:

阅读dispatch_queue_create文档:

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.

提交到队列的块按FIFO顺序一次执行一个。但是,请注意,提交给独立队列的块可以相互同时执行。

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函数释放它。提交到队列的任何挂起块都会保留对该队列的引用,因此在完成所有挂起块之前不会释放队列。

#2


4  

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.

在该方法运行之后,由于没有任何内容引用您创建的队列,并且从未调用dispatch_release(),因此它被视为泄漏。

#1


18  

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.

...当您不再需要访问队列时,应该调用dispatch_release。因为myQueue是局部变量,所以你必须在那里调用它。

Read dispatch_queue_create documentation:

阅读dispatch_queue_create文档:

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.

提交到队列的块按FIFO顺序一次执行一个。但是,请注意,提交给独立队列的块可以相互同时执行。

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函数释放它。提交到队列的任何挂起块都会保留对该队列的引用,因此在完成所有挂起块之前不会释放队列。

#2


4  

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.

在该方法运行之后,由于没有任何内容引用您创建的队列,并且从未调用dispatch_release(),因此它被视为泄漏。