---
title: "El cerrojo y su contador"
url: "https://www.mikefluff.com/es/experiments/bc-ch09-retain-release"
locale: "es"
author: "Mikhail Savchenko (Mike Fluff)"
updated: "2026-11-24"
description: "Conteo de referencias en Objective-C: fugas y caídas"
book: "https://www.mikefluff.com/es/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"
---

# El cerrojo y su contador

*Conteo de referencias en Objective-C: fugas y caídas*

Antes de 2011, quien escribía en Objective-C para Mac o iPhone llevaba de cabeza la cuenta de los dueños. Un release olvidado dejaba el objeto en memoria para siempre, uno de más destruía un objeto todavía en uso.

El código empieza como el archivo del autor: un cerrojo creado en una variable local y nunca entregado. Recorra una apertura paso a paso mirando el contador, o abra el programa cien veces. Con ARC el compilador reparte los mensajes, pero no puede saber que usted quería entregar el cerrojo al dispositivo.

_La página tiene un experimento interactivo que funciona en el navegador: https://www.mikefluff.com/es/experiments/bc-ch09-retain-release_

## Del libro

A su lado en el libro:

> 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.

- Leer el capítulo: [Chapter 9. retain/release](https://www.mikefluff.com/en/read/beautiful-computer/ch09) (inicio libre, el resto en el libro completo; Markdown: https://www.mikefluff.com/en/read/beautiful-computer/ch09.md)

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