__unsafe_unretained special identifiers, as well as ARC memory leaks, are some of the most important elements of the __unsafe_unretained identifier.

Click to the StackOverFlow Q&A page

A, problem,

The problem

Problem of translation

On Mac and iOS, unfreed Pointers often cause memory leaks. Traditionally, it is often extremely important to check your alloc, copy, and retain to make sure that each has a corresponding release.

The data link that accompanies Xcode 4.2 introduces automatic reference counting (ARC) from the latest version of the LLVM compiler, which completely solves this problem by letting the compiler manage memory. This is pretty cool, and it does reduce unnecessary, useless development time and prevent a lot of careless memory leaks that can be easily fixed with proper retention or release. Even automatic release pools need to be managed differently when you enable ARC for Mac and iOS applications (because you should no longer allocate your own NSAutoreleasePools).

But are there any other memory leaks that haven’t been prevented that I should be aware of?

As an extra, what is the difference between ARC on Mac OS X and iOS, and memory reclamation on Mac OS X?

Second, the answer

answer

Answer translation

The main memory-related issue that you still need to be aware of is circular references. This happens when one object has a strongly referenced pointer to another object, but the target pointer also has a strongly referenced pointer to the original object. A Even if all other references to these objects are removed, they still hold each other and will not be released. This can also happen indirectly, going back to the object chain of an earlier object by referencing the last object that might be in the chain.

This is why __unsafe_unretained and __weak ownership qualifiers exist. The former does not retain any object it points to, but leaves open the possibility that the object disappears and points to the wrong memory, while the latter does not retain the object and automatically sets it to nil when its target is freed. Of the two, __weak is usually preferred on platforms that support it.

You can use these qualifiers to perform things like proxies, in which case you do not want the object to retain its delegate object and may result in circular references.

A few other important memory-related issues are dealing with Core Foundation objects and using malloc() to allocate memory like char *. ARC doesn’t manage these types, only Objective-C objects, so you still have to deal with them yourself. Core Foundation types can be particularly tricky because sometimes they need to match Objective-C objects across Bridges, and vice versa. This means that you need to transfer control back and forth from ARC when bridging between Core Foundation types and Objective-C. Several keywords related to Bridges have been added, and Mike Ash describes various bridge cases in his lengthy ARC writing.

In addition, there are several other less frequent but still potentially problematic cases that will be covered in detail in the released specification.

Based on the fact that a strong pointer can keep many new behaviors of objects, much like garbage collection on the Mac. However, the technical underpinnings are very different. This style of memory management relies on the rigid retention/release rules we need to follow in Objective-C, rather than having a garbage collection process that runs regularly to clean up objects that are no longer pointed to.

ARC just repeats the memory management tasks we’ve been doing for years and shunt them to the compiler, so we don’t have to worry about it anymore. This way, you don’t have stop problems on the garbage collection platform or experience sawtooth memory configuration problems. I’ve experienced both in the Mac application for garbage collection and would love to see how they behave under ARC.

For more on garbage collection and ARC, check out Chris Lattner’s very interesting response to the Objective-C mailing list, which lists many of the benefits of ARC for Objective-C 2.0 garbage collection. I ran into several of the garbage collection problems he described.

In addition

  • Jane’s address book
  • Denver address