Search code examples
objective-ciosmemoryretain

Some doubts about memory-retain in ios applicato?


  1. What is the need for retain an NSObject in ios application?

  2. What is the difference between retainCount==1,retainCount=2,.....etc?

  3. How properties can handle retaining and releasing instance variable?

  4. when i am retain/releasing NSObject ,the retain count increate/decrease by one, what happens exactly in memory?


Solution

    1. Retaining an object indicates that you are taking ownership of that object. So you have to release it once after you are done with it.
    2. retainCount = 1 indicates that it is being strongly referenced from one place. If retainCount = 2, then it is being strongly referenced from two places.
    3. Properties, depending on whether it is a retain, copy or assign property, handles memory management differently
    4. Retain count only tells us how many string references are there for the object. As ling as it is greater than 0, the object is not removed from memory. Once it becomes 0, the obj is removed from memory (dealloc of tht object is called)

    EDIT:

    1. If it is a retain property, each time you set a retain property using the '.', then the old value is released, new value is retained and assigned to the property. The same happens with copy, just that the new value is sent a copy instead of retain. If it is an assign property, the new value is directly assigned to the property (no release, no retain)