IOS Memory Management

Source: Internet
Author: User

Block

Circular references

The circular reference of block is relatively easy to be neglected, and it is also comparatively difficult to check out the problem. Of course, Apple has already done a circular reference check at the level of the Xcode compilation, so the examination of this problem suddenly becomes no more difficult.

Simply say the principle of circular referencing:The owner of the block references itself within the block scope, so that the owner of the block will never be able to free up memory, and there is a circular reference to the memory leak. Here's an example to illustrate:

@interface objtest () {    void (^block) (); @end @implement objtest-(void) function {    = ^() {        ;    };}  @end

In this example, Objtest has a block object named block, and in this block, a member variable testvalue of Objtest is assigned. Thus, a circular reference was generated: Objtest->block->objtest.

The key to avoiding circular references is to break the closed loop. With only the ARC environment in mind, there is only one way I know to break this ring: use weak references to the owner within the block.

@interface objtest () {    void (^block) (); @end @implement objtest-(void) function {    __weak objtest* weakself = Self  ;     = ^() {        ;    };} @end

IOS Memory Management

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.