The difference between an iOS development arc and a non-arc code

Source: Internet
Author: User

is a memory management issue in IOS development: Here's a brief overview of what's going on, and as an iOS developer, or iOS development enthusiast, it's important to know:
The OBJECTIVE-C provides two memory management mechanisms, MRC (mannulreference counting) and arc (Automatic Reference counting), providing manual and automated management of memory to meet different requirements. In fact, arc internal mechanism principle is also originated from MRC, ARC is in IOS 5/mac OS X 10.7 start to import, use Xcode4.2 can use this function. ARC's primary purpose is to make the code concise, programming simplification, development more liking should also launch, reduce unnecessary small problem small negligence; As the name implies, automatic reference count management, the application of memory, the use and release process to the system automatically implemented, we can not be involved in the process, but in fact, the MRC principle , but it is the system to help us do the management;
MRC, manual reference Counter management, is in our application to a piece of memory, after use, to manually release, release mechanism involves counter problems, if not free memory, will cause memory waste, commonly known as memory leaks, and even caused a lot of unknown error results, this program has a great threat, but, when released, How to release, pay attention to what problems, very fastidious, this is the MRC inconvenience, but also the reason for Apple to launch arc;
The specific mechanism of MRC, the counter is what, in the process of the changes in the program, to what extent will release memory, how to operate; recommended to consult related documents;
MRC, in the code up and down form the main expression, when the object is called, to do retain operation, use finished to release, and finally to rewrite the Dealloc method, all objects of the class is released, so the MRC code will have Autorelease,retain, Release and other words,
ARC does not allow these words and should be done automatically by the system for these operations.

Reference counter
1. Methods related to memory management
1) alloc reference counter is automatically set to 1
2) Retain reference counter +1 returns the current instance object after +1
3) Release Reference counter-1, and not necessarily the release
4) Retaincount Gets the value of the reference counter
5) Dealloc The system is called automatically before the instance object is destroyed.
Be sure to tune [Super Dealloc]

Terminology related to memory management
1) Zombie Object: This object is destroyed, no longer used, no message can be sent to it
2) Wild pointer: A pointer to a zombie object (memory not available), sending a message to the wild pointer will have an uncontrolled effect.
3) NULL pointer: No pointer to any object, sending a message to a null pointer does not produce any behavior

Memory Management Principles
1. If you want to hold an object, you must be responsible for making a retain operation, referencing counter +1.
2. If you want to give up ownership of an object, be responsible for letting it do a release operation, referencing counter-1.

3. Who retain, who release.

In the end, if you are a novice, it is recommended to use MRC to write, so you can clearly understand the OC memory management mechanism, to avoid some unnecessary mistakes

The difference between an iOS development arc and a non-arc code

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.