The coming Delphi for iOS compiler will have a brand new memory management model for Delphi, ARC. This was mentioned in my previous blog post (has been 2 hectic weeks, sorry for not blogging for so long), and by Olaf Monien in some blog posts (including http://www.monien.net/delphi-xe-4-is-arc-for-everyone/ and http://www.monien.net/arc-what-if-i-call-free-will-i-get-tared-and-feathered/).

Here I don't want to and have no room for an in-depth discussion of ARC, just want to share some information. A detailed description will become available soon in an in-depth technical white paper I wrote and Embarcadero is going to release as the product ships. So this is just a summary.

1. Automatic Reference Counting is an alternative to Garbage Collection, in which a developers has more control and some extra responsibilities. It has been adopted, among others, by Apple for iOS development. You can read about ARC in Clang at http://clang.llvm.org/docs/AutomaticReferenceCounting.html.

2. In a simple scenario, you create an object, assign it to a local variable, and as the variable goes out of scope the object is destroyed. No need to write a Free call, and not need for a finally block to protect it.

3. In a more complex scenario, all standing references to an object are counted, when the count reaches 0, the object is destroyed.

4. If this looks like reference counting for interfaces in Delphi, this is indeed the case. Similar idea. In fact, you can now mix interfaces and object references, given the implementation is shared (while in the past this would easily cause memory problems)

5. You can have a reference that plays on different rules and doesn't increment the reference count. This is called a weak reference and marked with the [weak] attribute in the source code. This is used, for example, for a child object to refer back to the parent object, avoiding a reference cycle (which will break the reference count, as it never goes to zero).

5. You can force the execution of the destructor for an object, before it is destroyed, by calling the new special purpose DisposeOf function.

6. For backwards compatibility, DisposeOf calls Free on Win32/Mac. For forward compatibility, Free doesn't call the destructor, but sets the reference to nil on iOS, as described in Olaf's second blog post linked above. You can even keep FreeAndNil calls around (if you really, really want to; but don't tell Nick I suggested this).

7. As Olaf mentions (in the first post), this is for the iOS compiler and Simulator only, not for the Win/Mac compilers. It will be likely used for Android. How/if/when to make ARC available on the desktop platforms is under evaluation.

8. This is a big change, requires looking to existing code with care. There is a lot of backwards compatibility mixed in (like keeping your Free calls). But there are many scenarios requiring special coding (weak references, calls to DisposeOf, or just a change in the class architecture).

9. Once grasped, ARC really makes coding in Delphi easier... and more modern. And it is still a deterministic memory management style.

There is way more, in technical terms, to automatic reference counting in the upcoming Delphi for iOS compiler, Embarcadero's first Delphi compiler for ARM. This blog post is more of a teaser, than a technical introduction. More will follow. Stay tuned (and I promise to get back to blogging more).

- Written in Scotts Valley, California -