Show Categories & Top Posts

retain

In the Foundation framework for iOS and Mac the main container classes (NSString, NSArray, NSDictionary, and some others) have two states; Immutable and Mutable. If a class is immutable, it cannot be changed and is treated as ready only. When a class if mutable it can be modified after creation. It may seem that when passing one of these around it may be faster to retain it rather than copy it, because new memory does not need to be allocated. While this is true it can leave instabilities, see this example.

Xcode’s “Analyzer” is normally a great way to determine where most memory leaks will occur in your project without actually running it. If you follow Apple’s Memory Management Guidelines the Analyzer is pretty accurate. By no means is it a replacement for the Profiler, but offers some quick checks.

When using delegates in an object in Objective-C it is important that the delegate is only assigned within your object, and never retained. The reason for this is to prevent a retain loop, where two objects retain each other; they will never be released. The fix is simple, but can catch you off guard if you want to create an array or dictionary of delegates (using an NSDictionary or NSArray).