Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Do I ever have to call dispatch_release under ARC?

(But this question is totally different)

This non-ARC code example sets up a GCD-based timer and it calls dispatch_release for a dispatch_source_t object:

    __block BOOL done = NO;
    dispatch_source_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
    if (timer) {
        uint64_t milliseconds = 100ull;
        uint64_t interval = milliseconds * NSEC_PER_MSEC;
        uint64_t leeway = 10ull * NSEC_PER_MSEC;
        __block typeof(self) _self = self;

        dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), interval, leeway);
        dispatch_source_set_event_handler(timer, ^{
            //[_progressBar setProgress:exportSession.progress animated:YES];
            if (done) {
                dispatch_source_cancel(timer);
                dispatch_release(timer);
                _self.exportingMovieLabel.hidden = YES;
                _self.exportingProgress.hidden = YES;
            }
        });

        dispatch_resume(timer);
    }

I learned that you do not have to release queue objects under ARC. But other GCD objects like a dispatch source?

like image 367
openfrog Avatar asked Oct 18 '13 16:10

openfrog


2 Answers

No, provided that your deployment target is iOS 6.0 or Mac OS X 10.8 or later, as explained here.

All GCD objects are managed by ARC, so you don't need to explicitly manage their memory. What is true for dispatch_queue_t also applies to all the other GCD objects.

This is documented in <os/object.h> right above the definition of OS_OBJECT_HAVE_OBJC_SUPPORT.

By default, libSystem objects such as GCD and XPC objects are declared as Objective-C types when building with an Objective-C compiler. This allows them to participate in ARC, in RR management by the Blocks runtime and in leaks checking by the static analyzer, and enables them to be added to Cocoa collections.

You can also opt-out from this behavior using the -DOS_OBJECT_USE_OBJC=0 compiler flag.

like image 102
Gabriele Petronella Avatar answered Oct 22 '22 22:10

Gabriele Petronella


Here is compiler directive to add to your code.

// If GCD objects are treated as Objective C object then we do not need to call dispatch_release on those GCD object.
// Checking if OS_OBJECT_HAVE_OBJC_SUPPORT == 0 ensures we are only releasing GCD objects when they are NOT being treated as Objective C objects.
#if OS_OBJECT_HAVE_OBJC_SUPPORT == 0
    dispatch_release(timer);
#endif
like image 35
Kris Subramanian Avatar answered Oct 22 '22 23:10

Kris Subramanian