Get Updates By E-mail 

Article Written By:
Johann Dowa

Avoid Retain Cycles In Objective-C

  •  
  •  
  •  
  •  
  •  

Found an excellent set of rules from Matt Gallagher on how to avoid retain cycles in Objective-C. 

Uh-Oh A Class Is Still Retained Somewhere…

A problem that can occur in Objective-C is that when you start sharing objects between classes and increasing the retain count you can end up with a situation where one class inherits from another, and then when you want to release the object does not get released from memory because it is being retained by a class lower down the class hierarchy.

One Basic Rule

Now if this all sounds confusing, Matt explains everything well in his article, and has created a set of 5 rules.  Ultimately it comes down to one basic rule:

"an object may only retain something indefinitely if it is hierarchically senior. If you don’t know which object is senior, you must work it out before you retain. If there is no clear senior object — you should redesign so that there is."

For a more detailed detailed explanation (with diagrams) be sure to check out: Rules To Avoid Retain Cycles

[Via: Cocoa With Love]

 


  •  
  •  
  •  
  •  
  •  

Submit A Resource

Have you created a useful tutorial, library or tool for iOS development that you would like to get in front of our 300,000+ monthly page views from iOS developers?

You can submit the url here.

The resources we feel will appeal to our readers the most will be posted on the front page.

Comments