IOS ARC mechanism

Source: Internet
Author: User

IOS Arc Memory Auto-management mechanism, at present, almost a lot of projects will go with ARC, because it frees the memory of this physical work to dry, but, although ARC is very good, we still can not completely put memory management this thing behind the head.

How ARC works:

ARC is a pre-compilation step that automatically adds a retain/release/autorelease statement to our code.
ARC is not garbage collection, and the reference count doesn't go away, it just becomes automatic. It sounds like an afterthought, but if we think about how much of the objective-c is done by preprocessing the source file, you don't think so.
Here is an example:
That's what happens when we use arc.

  NSObject *obj = [[NSObject alloc]init];    Do any additional setup after loading the view, typically from a nib.

So with arc,

NSObject *obj = [[NSObject alloc]init]; [obj release];

There are some official introductions here.
ARC came up with some new rules: (source)
1. The object's alloc/init creates the object in the same way as before, but you must not call retain/release/autorelease/ Retaincount. They cannot be secretly called by selector: @selector (retain) and @selector (release) are forbidden.
2. Dealloc Method
Arc is called automatically for you and must not call Dealloc directly. However, if you need to release resources other than the instance variable, you can create a custom Dealloc method. But in this method, do not call [Super Dealloc]. Because ARC will help you tune it.
3. Declared properties
before arc, we used the assign/retain/copy parameter in the @property directive to tell the compiler how to manage the memory of these properties. After using arc, these parameters are void and use the Weak/strong two parameters instead.
4. Object pointers in C structures
are also prohibited. It is recommended in the documentation not to put them in the structure and put them in the class. Otherwise, arc doesn't know them. There may be some porting problems. However, ARC can be closed as a file. Refer to "Introducing incompatible arc code" below.
5. The code that replaces NSAutoreleasePool
compatible with @autoreleasepool can no longer use NSAutoreleasePool objects, but instead uses @autoreleasepool{} blocks. A good example:

int main(intchar *argv[]){  @autoreleasepool {    returnclass]));  }}
    1. Other
      Zone-based memory is gone (not at run time). Nsallocateobject and Nsdeallocateobject can no longer be used.
      7. You cannot give a property a name starting with new
      Ok! In terms of the ARC qualifier-declared properties, when we develop, we will determine the object properties we use according to the specific requirements.
      Under Arc, it is generally strong,week, that strong applications, and weak references,
      Strong strong references: @property(strong)UIView *bgView;
      Equivalent to all properties of the object, that is, the object is destroyed only if the object's strong property is freed.

IOS ARC mechanism

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.