Alan Quatermain

The Tumblog of one Jim Dovey, iOS Software Chief Architect at Kobo in Toronto, Ontario.
He Twitters, he has an , and can occasionally be found on LinkedIn or Facebook.
If you have a query, you can ask it here.

This blog contains personal opinions, and is not endorsed by any company.

3962386405

Fun with Objective-C: How to learn Cocoa

funwithobjc:

This is something any experienced Cocoa developer has been asked at least half a dozen times:

“What’s the best way to learn Objective-C/Cocoa?”

I have two answers to this question:

  1. Be curious
  2. Don’t be satisfied

He goes on to finish with the following epithet:

  1. Know what a pointer is.
  2. Memorize the memory management rules.
  3. Never invoke -retainCount.

The second item there is the one I most frequently hear is ‘really difficult’ to understand. It’s not. It’s really not. It looks like this:

  • Only the owner of an object needs to think about its memory management.
  • You only own an object if you get it from a method whose name begins with any one of:
    • alloc, including +allocWithZone:
    • new, including +newSomethingSomething
    • init, including -initWithSomeParameter (er, no, it doesn’t. I’m being stupid. Sorry.)
    • copy, including -copyWithZone: or -mutableCopy or -mutableCopyWithZone:
  • If the object doesn’t come from a method matching those rules, you don’t own it.

Edit: Specifically, the method must either be one of the above words, or begin with one followed by an uppercase letter. This means that +newSomething returns an owned reference, but +newestSomething does not. Thanks to Dave DeLong for reminding me of this via email.

It’s that simple. If you call something including alloc, new, init, or copy, then you must use -release (or -autorelease) to balance that call. If you don’t call one of those methods, you Just Don’t Care.

How about CoreFoundation? That’s even simpler: you own an object if it comes from a method containing Create. The same thing could be said about Copy, but CF’s copy constructors are always careful to frequently already contain Create, so we don’t need to. Anything named using Get returns an already-managed reference, so you don’t need a CFRelease for it.

Edit: I’m reminded by Peter Hosey that many CF-style APIs outside of CoreFoundation itself tend to use Copy without Create, for instance those in CoreGraphics. Note however that not everything that looks like a CF object really is one— many are just C wrappers around Carbon-era C++ code.

See? Easy. A list of four keywords in Objective-C code indicate the only times you need to ever think about memory management. And one keyword in CoreFoundation to tell you the same.

Now, there are exceptions— usually when something missed the boat, or when an existing API needed to be changed to fix an internal memory-management issue. The documentation will tell you about these very rare cases, but the best way to catch them for now is judicious use of Build & Analyze. The Clang compiler’s static analysis tool knows all about these rules, and can be told about any differences to them by any API. It will tell you explicitly if you’ve released something you don’t own, or have failed to release something you do own. Use it. Love it.

Memory management in Cocoa isn’t difficult. There are rules, but they’re simple and easy to remember. The main thing to remember is:

Don’t over-think it

118 notes

  1. casque-monster-beat reblogged this from funwithobjc
  2. thomas-1984 reblogged this from funwithobjc
  3. bellimbusto2 reblogged this from funwithobjc
  4. first-grade-reading reblogged this from funwithobjc
  5. ellenpage2 reblogged this from funwithobjc
  6. white-curtains reblogged this from funwithobjc
  7. candles-holders reblogged this from funwithobjc
  8. floor-pillow reblogged this from funwithobjc
  9. grill-grates reblogged this from funwithobjc
  10. mixer-blender reblogged this from funwithobjc
  11. smoker-recipes reblogged this from funwithobjc
  12. cricket-franchise reblogged this from funwithobjc
  13. cricket-master-dealer-chicago reblogged this from funwithobjc
  14. boynton-beach-real-estate reblogged this from funwithobjc
  15. core-games-download reblogged this from funwithobjc
  16. overweightdogs reblogged this from funwithobjc
  17. 1weddingfashion reblogged this from funwithobjc
  18. bestheartratemonitors reblogged this from funwithobjc
  19. comohacercrecerelpene reblogged this from funwithobjc
  20. shorttermcarinsuranceuk reblogged this from funwithobjc
  21. shorttermcarinsuranceuk reblogged this from funwithobjc
  22. mac-games-download reblogged this from funwithobjc
  23. shorttermcarinsuranceuk reblogged this from funwithobjc
  24. shorttermcarinsuranceuk reblogged this from funwithobjc
  25. hot-to-seo reblogged this from funwithobjc
  26. bug-repellent reblogged this from funwithobjc
  27. cat-adoptions reblogged this from funwithobjc
  28. quick-foods reblogged this from funwithobjc
  29. soda-drinks reblogged this from funwithobjc
  30. toys-for-dog reblogged this from funwithobjc
  31. asvab-test reblogged this from funwithobjc