Objective-C memory management under arc (1)

Source: Internet
Author: User

The automatic reference counting (ARC) is IOS 4.0 or later. You can choose to enable the memory management function when creating a project in xcode 4.2 or later. It seems to be a selectable function, but it has a great impact on programming after it is enabled, so it is worth a careful consideration.

 

First, as the name of the automatic reference counting indicates, automatic reference counting is not a garbage collection, not in Javascript, just declaring variables or members, the interpreter automatically determines whether to release it.

There are the following restrictions under Arc:

Release cannot be called,

Dealloc cannot be called directly or [Super dealloc]

The only intuitive method is

STR = nil;

However, even so, the subsequent Code cannot use STR to reference the original string object, and the memory used by the original string may not be released. For example:

Nsstring * strtest = [[nsstring alloc] initwithformat: @ "% @", @ "One !!! "];

Nsstring * strowned = strtest;

Strtest = nil;

Nslog (@ "% @", strowned );

 

The result is output one !!!

The reason is that all declared objects are strong by default. As long as the object is referenced once by the strong object, count (actual object counter) is added with 1, in the above example, strtest points to @ "One !!! "It was referenced once by strowned. Even if you are set to nil (release, Count minus one in the ARC world), the count is not 0, so the memory is not released, and until strowned = nil, @ "One !!! .

 

The release mechanism process is well understood in the above simple situations. Let's take a look at the following situation:

Nsstring * strtest = [[nsstring alloc] initwithformat: @ "% @", @ "One !!! "];

Nsstring * strtest2 = [[nsstring alloc] initwithformat: @ "% @", @ "Two !!! "];

Nsstring * strtest3 = [[nsstring alloc] initwithformat: @ "% @", @ "Three !!! "];

Nsstring * strtest4 = [[nsstring alloc] initwithformat: @ "% @", @ "Four !!! "];

Nsstring * strtest5 = [[nsstring alloc] initwithformat: @ "% @", @ "Five !!! "];

Nsstring * strtest6 = [[nsstring alloc] initwithformat: @ "% @", @ "Six !!! "];

Nsstring * strtest7 = [[nsstring alloc] initwithformat: @ "% @", @ "Seven !!! "];

Array = [[nsmutablearray alloc] init];

[Array addobject: strtest];

[Array addobject: strtest2];

[Array addobject: strtest3];

[Array addobject: strtest4];

[Array addobject: strtest5];

[Array addobject: strtest6];

[Array addobject: strtest7];

 

 

Execute somewhere

[Array removeallobjects];

 

Strtest ~ Is strtest7 released?

To answer this question, first answer: Is the array object referenced?

See the following statement:

Nsstring * strowned;

_ Weak nsstring * strweak;

_ Weak nsstring * strweak2;

Nsstring * strstrong;

 

Strowned = [array objectatindex: 0];

Strweak = [array objectatindex: 1];

Strweak2 = [array objectatindex: 2];

Strstrong = strweak2;

 

What are the output below?

Listing 3

Nslog (@ "% @", strowned );

Nslog (@ "% @", strweak );

Nslog (@ "% @", strweak2 );

Nslog (@ "% @", strstrong );

 

Run

[Array removeallobjects];

Then, the output is as follows:

 

 

It is interesting to compare the results of strweak and strweak2 here. It is also declared as weak. After strweak2 is referenced by strstrong, the object used cannot be referenced by the source (array here) released, which may be the cause of errors in complex projects.

 

Finally, if the string objects in array are referenced by members of another class

Myobj = [[myobject alloc] init];

Myobj. strstrong = strtest6;

Myobj. strweak = strtest7;

 

Run

[Array removeallobjects];

After that, what is the execution result of listing 3? Before answering this question, consider whether myobj is strong or weak. What is the difference?

Related Article

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.