Two methods of releasing objects under arc in IOS6

Source: Internet
Author: User

The use of ARC has undoubtedly greatly reduced the pressure on the programmer to manage memory. You don't have to write release/autorelease code anymore, you don't have to write the Dealloc method anymore. But that doesn't mean programmers don't need memory management. For example, you need to release an object in the Viewdidunload method in case you don't have memory to recycle when a memory warning occurs. Of course, the Arc managed object is released in a different way from MRC. For Arc managed objects, you have to use two types of release that arc-specific:

[Self setojbect:nil];

Or:

Self.object=nil;

Here, object is the name you want to release.

The two modes of release are essentially equivalent, and even many books claim that they are completely equivalent. For programmers with java/c++/c# development experience, the call to point syntax is clearly more cordial.

But I have to tell you that the two are not exactly equal. [Self Setobject:nil] is more secure in cases where object objects are not initialized (for nil).

If object is not initialized, the Self.object=nil call causes a "send message to the destroyed object" error and the program crashes. And [Selfsetobject:nil] is not at all.

Note: The ipad's memory management seems to be much broader, and some code that works on the ipad can cause memory management to fail on the IPhone.

This is because ARC adds an "Auto balance" code when synthesizing the access method of the Property object, which automatically detects the Alloc/release invocation of the object and balances it in an extremely intelligent way.

Therefore, for those accustomed to java/c++ language "." In the case of a programmer with rampant syntax, it should be possible to discard the prejudices of o-c and return to the "right" of the message invocation syntax.

Especially for the release of objects, be careful to use the [self setobject:nil] approach, instead of using the "." Grammar. Because, you can send a "message" to a nil object, but you cannot assign a value to a nil object.

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.