ARC: Automatic Reference Counting Automatic reference counting

ARC several key points:

Retain count +1 when the object is created and retain count -1 when the object is released. When retain count is 0, the object is destroyed. Objects added to the program with autoReleasepool are automatically added with the AutoRelease method. If the object reference count is 0, it is destroyed. So what problem was ARC created to solve? This goes back to the days of MRC manual memory management.

Disadvantages of memory management under MRC:

1. When we want to free a heap, we first make sure that Pointers to the heap are released. (Avoid early release)

2. The first step is to determine which Pointers point to the same heap. These Pointers can only be freed once. (under MRC that is, who creates, who releases, avoid duplicate releases)

3. In modular operations, objects may be created and used by multiple modules, and it is uncertain who will finally release them.

4. In multi-threaded operations, it is uncertain which thread will be used up last

Using the ARC, developers no longer need to manually retain/release/autorelease. The compiler automatically inserts the corresponding code and, in conjunction with the Objective C Runtime, implements automatic reference counting.

In Objective C, there are three types that ARC applies to:

  • block
  • Objective object, ID, Class, NSError*, etc
  • The type marked by attribute((NSObject)).

Things like double *,CFStringRef, etc. are not ARC friendly and still require manual memory management.