生成调度源计时器事件时发生内存泄漏

发布于 2024-10-31 11:11:31 字数 686 浏览 1 评论 0原文

我们使用调度队列来生成计时器事件。 执行该任务的代码:

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 技术交流群。

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

发布评论

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

评论(1

羞稚 2024-11-07 11:11:31

dispatch_source_set_timer(3) Mac OS X 手册页面

所有计时器将无限重复,直到
调用dispatch_source_cancel()。

如何为计时器调用dispatch_source_cancel()和dispatch_release()?

调度源定时器示例:

dispatch_source_t timer = dispatch_source_create(
    DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));

dispatch_source_set_timer(timer,
    dispatch_time(DISPATCH_TIME_NOW, 1ull * NSEC_PER_SEC),
        DISPATCH_TIME_FOREVER, 1ull * NSEC_PER_SEC);

dispatch_source_set_event_handler(timer, ^{
    NSLog(@"wakeup!");

    dispatch_source_cancel(timer);
});

dispatch_source_set_cancel_handler(timer, ^{
    NSLog(@"canceled");

    dispatch_release(timer);
});

dispatch_resume(timer);

dispatch_source_set_timer(3) Mac OS X Manual Page

All timers will repeat indefinitely until
dispatch_source_cancel() is called.

How do you call dispatch_source_cancel() and dispatch_release() for the timer?

Dispatch source timer example:

dispatch_source_t timer = dispatch_source_create(
    DISPATCH_SOURCE_TYPE_TIMER, 0, 0,
        dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0));

dispatch_source_set_timer(timer,
    dispatch_time(DISPATCH_TIME_NOW, 1ull * NSEC_PER_SEC),
        DISPATCH_TIME_FOREVER, 1ull * NSEC_PER_SEC);

dispatch_source_set_event_handler(timer, ^{
    NSLog(@"wakeup!");

    dispatch_source_cancel(timer);
});

dispatch_source_set_cancel_handler(timer, ^{
    NSLog(@"canceled");

    dispatch_release(timer);
});

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