An explanation of the arc principle

Source: Internet
Author: User

The essence of 1,arc

Arc essence is the direct application of NSAutoreleasePool,

@autorelease {

Return Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));

}

is actually:

nsautoreleasepool* pool = [[NSAutoreleasePool alloc] init];

int iRet = Uiapplicationmain (argc, argv, Nil, Nsstringfromclass ([Appdelegate class]));

[Pool release];

Then, after the various allocrelease, the corresponding refcount are set to 0, after their reference range, on the back of the NSAutoreleasePool automatically released, as follows:

nsstring* str = [[[NSString alloc] init] autorelease];

.......

Where STR has been autorelease, and when the life cycle is over, STR is released directly.

The principle of 2,nsautoreleasepool

Autoreleasepoolpage is a C + + implemented class.

typedef struct{magic_t const Magic;id* Next;pthread_t* thread;autoreleasepoolpage* Parent;autoreleasepoolpage* Child;uint32_t const depth;uint32_t Hiwat;}among them:1,magic is the magic number, dividing the memory boundaries, the data structure at the beginning;2,next, is the boundary that points to the Autoreleasepool;3,thread, is the thread of the Autoreleasepool, each thread has its ownAutoreleasepool;4,Parent, child, used to create a linked list, a autoreleasepool is not necessarily sufficient; each nsautoreleasepool is 4096bytes, enough to apply for a new nsautoreleasepool, with child, Parent connection, see: 1, "class instance occupies memory" is the data interface NSAutoreleasePool space, 2, "ID OBJX", is the object pointer of each application, 3,next, refers to the top of the stack, the object is how to apply?

In the picture, this page joins a Autorelease object will be full (that is, next pointer immediately to the top of the stack), this time to do the above-mentioned operation, set up the next Page object, and this page linked list after the connection is completed, The next pointer of the new page is initialized at the bottom of the stack (begin position), and then continues to add new objects to the top of the stack.

So, sending a-autorelease message to an object is to add the object to the position of the current Autoreleasepoolpage's stack top next pointer.

How is the object freed? Whenever a Objc_autoreleasepoolpush call is made, runtime add a Sentinel object to the current autoreleasepoolpage with a value of 0 (that is, nil), then this page becomes the following:

The return value of the Objc_autoreleasepoolpush is exactly the address of the Sentinel object, which is Objc_autoreleasepoolpop (Sentinel object) as the entry, and thus:

1. Locate the page where the Sentinel object is located, based on the address of the incoming Sentinel object

2. In the current page, send the release message once for all Autorelease objects inserted later than the Sentinel object, and move the next pointer back to the correct position

3. Supplement 2: From the newly added object has been cleared forward, you can move forward across several page, until the Sentinel page

Just after the objc_autoreleasepoolpop was executed, it turned out to look like this:

Nested Autoreleasepool

Know the above principle, nested autoreleasepool is very simple, pop time will always release to the last push position, multi-layered pool is more than Sentinel object, just like peeling onions, each layer, do not affect each other.

An explanation of the arc principle

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.