---
title: "The lock and its counter"
url: "https://www.mikefluff.com/en/experiments/bc-ch09-retain-release"
locale: "en"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-24"
description: "Reference counting in Objective-C: leaks and crashes"
book: "https://www.mikefluff.com/en/books/beautiful-computer"
alternates:
  - locale: "en"
    url: "https://www.mikefluff.com/en/experiments/bc-ch09-retain-release"
    markdown: "https://www.mikefluff.com/en/experiments/bc-ch09-retain-release.md"
  - locale: "ru"
    url: "https://www.mikefluff.com/ru/experiments/bc-ch09-retain-release"
    markdown: "https://www.mikefluff.com/ru/experiments/bc-ch09-retain-release.md"
  - locale: "pt"
    url: "https://www.mikefluff.com/pt/experiments/bc-ch09-retain-release"
    markdown: "https://www.mikefluff.com/pt/experiments/bc-ch09-retain-release.md"
  - locale: "es"
    url: "https://www.mikefluff.com/es/experiments/bc-ch09-retain-release"
    markdown: "https://www.mikefluff.com/es/experiments/bc-ch09-retain-release.md"
---

# The lock and its counter

*Reference counting in Objective-C: leaks and crashes*

Before 2011 every Objective-C programmer on the Mac or the iPhone kept the count of owners in the head. A forgotten release left an object in memory for good, an extra one destroyed an object still in use.

The code starts like the author’s file: a lock made into a local variable and never handed over. Step through one opening and watch the counter, or open the program a hundred times. With ARC the compiler places the messages, but it cannot know that you meant to give the lock to the device.

_The page has an interactive experiment that runs in a browser: https://www.mikefluff.com/en/experiments/bc-ch09-retain-release_

## From the book

Beside it in the book:

> It worked only at the second attempt. At the developers’ conference in June 2011, three months after my lock, they showed ARC, automatic reference counting. The rules of ownership stayed as they were, but the retain and release calls were now placed by the compiler, which reads the program and, unlike a human being, does not lose count. On the fifteenth of June Chris Lattner told a mailing list that the code of this machinery had been made public, and Xcode 4.2 acquired a menu item that converted an existing project to the new order.
>
> My project did not live to see that menu item. I last touched it in July 2011, and the lock in it is leaking to this day.

- Read the chapter: [Chapter 9. retain/release](https://www.mikefluff.com/en/read/beautiful-computer/ch09) (opening free, rest in the full book; Markdown: https://www.mikefluff.com/en/read/beautiful-computer/ch09.md)

- Book: [A Lovely Computer](https://www.mikefluff.com/en/books/beautiful-computer)
