Sobes.tech
Junior — Middle

Can you explain how the MRC mechanism functions?

sobes.tech AI

Answer from AI

MRC (Manual Reference Counting) is a memory management mechanism in iOS where the programmer is responsible for manually managing reference counts on objects.

Working principle:

  • Each object has a reference count.
  • When an object is created or assigned, the count increases (retain).
  • When a reference is released, the count decreases (release).
  • When the count reaches zero, the object is destroyed.

Example in Objective-C:

NSObject *obj = [[NSObject alloc] init]; // count = 1
[obj retain]; // count = 2
[obj release]; // count = 1
[obj release]; // count = 0, object is deleted

It is important to manually call retain and release to avoid memory leaks or premature object deletion. Modern iOS projects more commonly use ARC (Automatic Reference Counting), which automates this process.