iOS Development Series-arc Shallow solution

Source: Internet
Author: User

First, what is ARC?

The so-called arc is automatic Reference counting, which is the automatic reference count. ARC was introduced from the iOS5. The arc mechanism was introduced to simplify the memory management of the development process. The arc mechanism is more automated relative to the previous MRC (Manual Reference counting). In the ARC development process, developers only need to consider the use of strong/weak, no longer have to consider when the object to Retain,release/autorealease. Using arc generally does not reduce the efficiency of the program.

Ii. working principle of Arc

An important principle of arc is that it will not be destroyed as long as an object is strong pointed to by either pointer. If the object is not pointed to by any strong pointer, it will be destroyed .

ARC is based on the reference count, and when an object is pointed to by a strong pointer, it counts +1. When no strong pointer is pointed, its count is 0, and the object is destroyed. As long as an object has at least one strong pointer pointing, it will not be destroyed. But arc is prone to a strong Reference Cycle, so that even though both Addbook and Entry are no longer in use, because of the arc mechanism, both objects have strong pointers to each other, so neither of these objects will be recycled. This causes the memory to not be freed.

For the above scenario, there is a workaround: introduce weak in one of the objects, replacing its strong

After the introduction of weak, when the entry is used, since the pointer to Addrbook does not have a strong pointer, so Addrbook will be released first, and then because Addrbook is released, the entry pointer to strong will also be destroyed, There is no strong pointer to entry at this point, so entry will be released as well. This does not occur when memory cannot be released.

Here is a question, when should use strong, when should use weak? Look at the following parsing:

, Viewcontroller directly hold view, so Viewcontroller should have a strong point to view. Similarly, view directly holds subviews, so should also have strong point to subviews. Because Viewcontroller to use Subviews object, but do not want to directly hold subviews, so have to weak point to subviews. In this case, you can use the Subviews object without changing the view's holding relationship in Viewcontroller. A general rule can be drawn: for a relationship that has a direct hold, the holder is directed through the strong to the holder. In the case of an indirect holder, the indirect holder is directed through the weak to the indirect holder.

Reference article: Http://www.raywenderlich.com/5773/beginning-arc-in-ios-5-tutorial-part-2

http://onevcat.com/2012/06/arc-hand-by-hand/

iOS Development Series-arc Shallow solution

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.