Simple Block & memory leak issues with IOS

Source: Internet
Author: User

NOTE: block is referenced with copy

typedef int (^myblock) (int, int);

void Play ()

{

NSLog (@ "Play mp3");

}

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

{

@autoreleasepool {

Myblock Sumblock = ^ (int a, int b) {

return a + B;

};

Myblock Minusblock = ^ (int a, int b) {

return a-B;

};

int r = Minusblock (10, 2);

NSLog (@ "%d", r);

}

return 0;

}
——————————————————————————————

Memory leak problem: The block inside refers to self, and self strong pointer to block. So that self cannot be destroyed.
Workaround:
__weak typeof (self) SELFVC = self;
Define a self alias and use the Ruo pointer to reference the
————————————————————————————————

Note: When a block is defined, it is built in block

A copy (copy) of the contents of the current local variable is subsequently modified to the value of the variable, focusing on

Does not affect the values in the block

If you need to keep local variables in the block
If you want to modify a local variable outside the block, you need to use the __block keyword __block BOOL flag = NO;

————————————————

Simple Block & memory leak issues with IOS

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.