生成调度源计时器事件时发生内存泄漏
我们使用调度队列来生成计时器事件。 执行该任务的代码:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); if (!timer) return self; dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval * NSEC_PER_SEC, 5 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ //Some work… });
,只是当我们运行探查器时,我们看到这些方法中存在大量内存泄漏:
- dispatch_source_createdispatch_source_set_timerdispatch_source_set_event_handler
- 以下是
- 这工作得很好
我们已确保使用dispatch_release()方法释放计时器。
如果我们在上面的代码中犯了任何错误,有人可以告诉我们吗?另外,如果您能指出计时器事件生成的任何示例,那将会很有帮助。
We are using dispatch queues to generate timer events. Following is the code which does the task:
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0); timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, queue); if (!timer) return self; dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval * NSEC_PER_SEC, 5 * NSEC_PER_SEC); dispatch_source_set_event_handler(timer, ^{ //Some work… });
This works very well except that when we run the profiler, we see a lot of memory leaks from these methods:
- dispatch_source_create
- dispatch_source_set_timer
- dispatch_source_set_event_handler
We had made sure that timer is released using dispatch_release() method.
Can someone please let us know if there is any mistake we are doing in the code above? And also if you can point out any example of timer event generation, it would be helpful.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
dispatch_source_set_timer(3) Mac OS X 手册页面
如何为计时器调用dispatch_source_cancel()和dispatch_release()?
调度源定时器示例:
dispatch_source_set_timer(3) Mac OS X Manual Page
How do you call dispatch_source_cancel() and dispatch_release() for the timer?
Dispatch source timer example: