Archives: May 2012

An In-depth Look At Manual Memory Management In Objective-C

— Category: Cocoa

It feels like fleets of new developers dash themselves upon the rocky shores of Objective-C memory management every day on StackOverflow. I can't bear to write the same answers over and over again, so this article will be my final, unabridged explanation of: retain, release, autorelease, alloc, dealloc, copy, and NSAutoreleasePool. There will be some rules to memorize, but we will also take a journey under the hood for a deeper understanding.

This article is intended for people who are new to Objective-C, or who never fully learnt how manual reference counting works. It does assume that you have some programming experience, possibly in another language. If you are a beginner programmer and Objective-C is your first language, I will try to keep the explanations clear, but it may become confusing the more in-depth we get.

We will be covering:

  • A Quick Mention Of ARC And Garbage Collection
  • Coming From C/C++
  • Coming From a Garbage Collected Language (Java, Python, C#, etc.)
  • What Is Reference Counting?
  • How Does Reference Counting Work In Objective-C?
  • NSAutoreleasePool And autorelease
  • Common Mistakes

If something is not explained, or not explained very well, post a question in the comments and I'll expand the article to cover it.

Resource Acquisition is Initialisation (RAII) Explained

— Category: Software Design

In the competition to make the worst acronym, RAII probably comes second after HATEOS. Nevertheless, it is an important concept because it allows you to write safer code in C++ — a harsh, unforgiving language that is all too happy to help you shoot yourself in the foot.

This article will explain exception-safety and common pitfalls in C++. As we work out how to avoid these problems, we will accidentally discover RAII. Then, we will finish by defining exactly was RAII is, and where it is already being used.