(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?
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With