Monday, November 22, 2010

10 Ways to Manage Pointers(Memory) in Objective C

1, use release; When you use alloc and init to create an object you will need to send a release message when you are finished
2, use retain;  Objects that are returned through functions are usually autoreleased so you must send a retain message to them to keep them around or for a longer time period
3, smart collections; Adding objects to collections like navigation controllers, sub-views, arrays and dictionaries automatically sends a retain message to the object.
4, auto collections release; Releasing collections automatically sends a release message to every object in the collection.
5; use dealloc; When you use properties in your class definitions with the retain keyword the system is creating code for your (in the background) that automatically retains the object. This is why you need to
release these objects in the dealloc method.
6, The dealloc method is executed when the retain count of an object reaches zero.
7, Check the nullable before reference it; One huge problem that occurs when objects attempt to access another object that has been released from memory is that this causes the app to crash.
8, Another memory problem you will encounter is memory leaking,means orphan objects never been released but still in memory.
9, Tools can help you; Learn how to use Instruments and NSZombie to diagnose and fix these memory problems.
10,Safty to use C types. When you are working with things like int, BOOL or double you do not need to worry about the retain and release pattern.

No comments:

Post a Comment