Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Autoreleasepool and dispatch_async

I read the article about GCD, and there is an example:

dispatch_queue_t bgQueue = myQueue;
dispatch_async(dispatch_get_main_queue(), ^{
    NSString *stringValue = [[[textField stringValue] copy] autorelease];
    dispatch_async(bgQueue, ^{
        // use stringValue in the background now
    });
});

If i place that method in click handler (which will be called in the autoreleasepool), will i loss stringValue, because autoreleasepool will be destroyed after click event?

like image 414
INs Avatar asked Dec 21 '25 05:12

INs


1 Answers

Inside that inner block? No, you won’t lose that value. When an Objective-C object variable (which hasn’t been declared as __block) is referenced inside a block and the block is copied, that object is automatically retained. When the block is released, that object will be released, too. dispatch_async() is responsible for copying and releasing the block.


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!