[libdispatch-dev] Block not released after dispatch_source_set_event_handler

Allan Odgaard lists+libdispatch at simplit.com
Sat Feb 23 08:04:12 PST 2013

I am seeing an issue where the block given to dispatch_source_set_event_handler appear not to be released when a new handler is set.

If I build and run the code below:

   xcrun clang++ -std=c++11 -stdlib=libc++ test.cc

It outputs the ‘create’ line, but never any ‘destroy’.

If I don’t call dispatch_source_set_event_handler then I see the expected ‘destroy’. Alternatively I can call an extra Block_release(), which also results in the shared pointer being destroyed.

This is on Mac OS X 10.8.2 Build 12C60 using Apple LLVM version 4.2 (clang-425.0.24) (based on LLVM 3.2svn).


#include <dispatch/dispatch.h>
#include <Block.h>
#include <stdio.h>
#include <memory>

void setup_dispatch_source ()
   struct record_t
      record_t ()  { fprintf(stderr, "%p: create\n", this); }
      ~record_t () { fprintf(stderr, "%p: destroy\n", this); }

   std::shared_ptr<record_t> record(new record_t);
   auto block = Block_copy(^(){ fprintf(stderr, "%p\n", record.get()); });

   dispatch_source_t source = dispatch_source_create(DISPATCH_SOURCE_TYPE_VNODE, open("/tmp", O_EVTONLY, 0), DISPATCH_VNODE_REVOKE, dispatch_get_main_queue());
   dispatch_source_set_event_handler(source, block);
   dispatch_source_set_event_handler_f(source, NULL);


int main (int argc, char const* argv[])
   return 0;

More information about the libdispatch-dev mailing list