| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 1 | <html> | 
|  | 2 | <head> | 
|  | 3 | <title>Objective-C Automatic Reference Counting (ARC)</title> | 
|  | 4 | <link type="text/css" rel="stylesheet" href="../menu.css" /> | 
|  | 5 | <link type="text/css" rel="stylesheet" href="../content.css" /> | 
|  | 6 | <style type="text/css"> | 
|  | 7 | /* Collapse the items in the ToC to the left. */ | 
|  | 8 | div#toc ul { | 
|  | 9 | padding-left: 0 | 
|  | 10 | } | 
|  | 11 |  | 
|  | 12 | /* Rationales appear in italic. */ | 
|  | 13 | div.rationale { | 
|  | 14 | font-style: italic | 
|  | 15 | } | 
|  | 16 |  | 
| John McCall | f3d08a6 | 2011-06-18 07:31:30 +0000 | [diff] [blame^] | 17 | div.rationale em { | 
|  | 18 | font-style: normal | 
|  | 19 | } | 
|  | 20 |  | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 21 | div h1 { font-size: 2em; margin: .67em 0 } | 
|  | 22 | div div h1 { font-size: 1.5em; margin: .75em 0 } | 
|  | 23 | div div div h1 { font-size: 1.17em; margin: .83em 0 } | 
|  | 24 | div div div div h1 { margin: 1.12em 0 } | 
|  | 25 |  | 
|  | 26 | span.term { font-style: italic; font-weight: bold  } | 
|  | 27 | </style> | 
|  | 28 |  | 
|  | 29 | <script lang="javascript"> | 
|  | 30 | /// A little script to recursively build a table of contents. | 
|  | 31 | function buildTOC(div, toc, ancestry) { | 
|  | 32 | var children = div.childNodes; | 
|  | 33 | var len = children.length; | 
|  | 34 |  | 
|  | 35 | var childNumber = 0; | 
|  | 36 |  | 
|  | 37 | var list = null; | 
|  | 38 | for (var i = 0; i < len; ++i) { | 
|  | 39 | var child = children[i]; | 
|  | 40 | if (child.nodeName != "DIV") continue; | 
|  | 41 | if (child.getAttribute("class") == "rationale") continue; | 
|  | 42 | if (child.id == "toc") continue; | 
|  | 43 |  | 
|  | 44 | // Okay, we're actually going to build a list node. | 
|  | 45 | if (list === null) list = document.createElement("ul"); | 
|  | 46 |  | 
|  | 47 | var childAncestry = ancestry + ++childNumber + "."; | 
|  | 48 |  | 
|  | 49 | var headerNode = child.childNodes[1]; | 
|  | 50 | var title = headerNode.innerHTML; | 
|  | 51 | headerNode.insertBefore(document.createTextNode(childAncestry + " "), | 
|  | 52 | headerNode.firstChild); | 
|  | 53 |  | 
|  | 54 | var item = document.createElement("li"); | 
|  | 55 | item.appendChild(document.createTextNode(childAncestry + " ")); | 
|  | 56 |  | 
|  | 57 | var anchor = document.createElement("a"); | 
|  | 58 | anchor.href = "#" + child.id; | 
|  | 59 | anchor.innerHTML = title; | 
|  | 60 | item.appendChild(anchor); | 
|  | 61 |  | 
|  | 62 | buildTOC(child, item, childAncestry); | 
|  | 63 |  | 
|  | 64 | list.appendChild(item); | 
|  | 65 | } | 
|  | 66 | if (list) toc.appendChild(list); | 
|  | 67 | } | 
|  | 68 |  | 
|  | 69 | function onLoad() { | 
|  | 70 | var toc = document.getElementById("toc"); | 
|  | 71 | var content = document.getElementById("content"); | 
|  | 72 | buildTOC(content, toc, ""); | 
|  | 73 | } | 
|  | 74 | window.onload = onLoad; | 
|  | 75 |  | 
|  | 76 | </script> | 
|  | 77 | </head> | 
|  | 78 | <body> | 
|  | 79 |  | 
|  | 80 | <!--#include virtual="../menu.html.incl"--> | 
|  | 81 |  | 
|  | 82 | <div id="content"> | 
|  | 83 | <h1>Automatic Reference Counting</h1> | 
|  | 84 |  | 
|  | 85 | <div id="toc"> | 
|  | 86 | </div> | 
|  | 87 |  | 
|  | 88 | <div id="meta"> | 
|  | 89 | <h1>About this document</h1> | 
|  | 90 |  | 
|  | 91 | <div id="meta.purpose"> | 
|  | 92 | <h1>Purpose</h1> | 
|  | 93 |  | 
|  | 94 | <p>The first and primary purpose of this document is to serve as a | 
|  | 95 | complete technical specification of Automatic Reference Counting. | 
|  | 96 | Given a core Objective-C compiler and runtime, it should be possible | 
|  | 97 | to write a compiler and runtime which implements these new | 
|  | 98 | semantics.</p> | 
|  | 99 |  | 
|  | 100 | <p>The secondary purpose is to act as a rationale for why ARC was | 
|  | 101 | designed in this way.  This should remain tightly focused on the | 
|  | 102 | technical design and should not stray into marketing speculation.</p> | 
|  | 103 |  | 
|  | 104 | </div> <!-- meta.purpose --> | 
|  | 105 |  | 
|  | 106 | <div id="meta.background"> | 
|  | 107 | <h1>Background</h1> | 
|  | 108 |  | 
|  | 109 | <p>This document assumes a basic familiarity with C.</p> | 
|  | 110 |  | 
|  | 111 | <p><span class="term">Blocks</span> are a C language extension for | 
|  | 112 | creating anonymous functions.  Users interact with and transfer block | 
|  | 113 | objects using <span class="term">block pointers</span>, which are | 
|  | 114 | represented like a normal pointer.  A block may capture values from | 
|  | 115 | local variables; when this occurs, memory must be dynamically | 
|  | 116 | allocated.  The initial allocation is done on the stack, but the | 
|  | 117 | runtime provides a <tt>Block_copy</tt> function which, given a block | 
|  | 118 | pointer, either copies the underlying block object to the heap, | 
|  | 119 | setting its reference count to 1 and returning the new block pointer, | 
|  | 120 | or (if the block object is already on the heap) increases its | 
|  | 121 | reference count by 1.  The paired function is <tt>Block_release</tt>, | 
|  | 122 | which decreases the reference count by 1 and destroys the object if | 
|  | 123 | the count reaches zero and is on the heap.</p> | 
|  | 124 |  | 
|  | 125 | <p>Objective-C is a set of language extensions, significant enough to | 
|  | 126 | be considered a different language.  It is a strict superset of C. | 
|  | 127 | The extensions can also be imposed on C++, producing a language called | 
|  | 128 | Objective-C++.  The primary feature is a single-inheritance object | 
|  | 129 | system; we briefly describe the modern dialect.</p> | 
|  | 130 |  | 
|  | 131 | <p>Objective-C defines a new type kind, collectively called | 
|  | 132 | the <span class="term">object pointer types</span>.  This kind has two | 
|  | 133 | notable builtin members, <tt>id</tt> and <tt>Class</tt>; <tt>id</tt> | 
|  | 134 | is the final supertype of all object pointers.  The validity of | 
|  | 135 | conversions between object pointer types is not checked at runtime. | 
|  | 136 | Users may define <span class="term">classes</span>; each class is a | 
|  | 137 | type, and the pointer to that type is an object pointer type.  A class | 
|  | 138 | may have a superclass; its pointer type is a subtype of its | 
|  | 139 | superclass's pointer type.  A class has a set | 
|  | 140 | of <span class="term">ivars</span>, fields which appear on all | 
|  | 141 | instances of that class.  For every class <i>T</i> there's an | 
|  | 142 | associated metaclass; it has no fields, its superclass is the | 
|  | 143 | metaclass of <i>T</i>'s superclass, and its metaclass is a global | 
|  | 144 | class.  Every class has a global object whose class is the | 
|  | 145 | class's metaclass; metaclasses have no associated type, so pointers to | 
|  | 146 | this object have type <tt>Class</tt>.</p> | 
|  | 147 |  | 
|  | 148 | <p>A class declaration (<tt>@interface</tt>) declares a set | 
|  | 149 | of <span class="term">methods</span>.  A method has a return type, a | 
|  | 150 | list of argument types, and a <span class="term">selector</span>: a | 
|  | 151 | name like <tt>foo:bar:baz:</tt>, where the number of colons | 
|  | 152 | corresponds to the number of formal arguments.  A method may be an | 
|  | 153 | instance method, in which case it can be invoked on objects of the | 
|  | 154 | class, or a class method, in which case it can be invoked on objects | 
|  | 155 | of the metaclass.  A method may be invoked by providing an object | 
|  | 156 | (called the <span class="term">receiver</span>) and a list of formal | 
|  | 157 | arguments interspersed with the selector, like so:</p> | 
|  | 158 |  | 
|  | 159 | <pre>[receiver foo: fooArg bar: barArg baz: bazArg]</pre> | 
|  | 160 |  | 
|  | 161 | <p>This looks in the dynamic class of the receiver for a method with | 
|  | 162 | this name, then in that class's superclass, etc., until it finds | 
|  | 163 | something it can execute.  The receiver <q>expression</q> may also be | 
|  | 164 | the name of a class, in which case the actual receiver is the class | 
|  | 165 | object for that class, or (within method definitions) it may | 
|  | 166 | be <tt>super</tt>, in which case the lookup algorithm starts with the | 
|  | 167 | static superclass instead of the dynamic class.  The actual methods | 
|  | 168 | dynamically found in a class are not those declared in the | 
|  | 169 | <tt>@interface</tt>, but those defined in a separate | 
|  | 170 | <tt>@implementation</tt> declaration; however, when compiling a | 
|  | 171 | call, typechecking is done based on the methods declared in the | 
|  | 172 | <tt>@interface</tt>.</p> | 
|  | 173 |  | 
|  | 174 | <p>Method declarations may also be grouped into | 
|  | 175 | <span class="term">protocols</span>, which are not inherently | 
|  | 176 | associated with any class, but which classes may claim to follow. | 
|  | 177 | Object pointer types may be qualified with additional protocols that | 
|  | 178 | the object is known to support.</p> | 
|  | 179 |  | 
|  | 180 | <p><span class="term">Class extensions</span> are collections of ivars | 
|  | 181 | and methods, designed to allow a class's <tt>@interface</tt> to be | 
|  | 182 | split across multiple files; however, there is still a primary | 
|  | 183 | implementation file which must see the <tt>@interface</tt>s of all | 
|  | 184 | class extensions. | 
|  | 185 | <span class="term">Categories</span> allow methods (but not ivars) to | 
|  | 186 | be declared <i>post hoc</i> on an arbitrary class; the methods in the | 
|  | 187 | category's <tt>@implementation</tt> will be dynamically added to that | 
|  | 188 | class's method tables which the category is loaded at runtime, | 
|  | 189 | replacing those methods in case of a collision.</p> | 
|  | 190 |  | 
|  | 191 | <p>In the standard environment, objects are allocated on the heap, and | 
|  | 192 | their lifetime is manually managed using a reference count.  This is | 
|  | 193 | done using two instance methods which all classes are expected to | 
|  | 194 | implement: <tt>retain</tt> increases the object's reference count by | 
|  | 195 | 1, whereas <tt>release</tt> decreases it by 1 and calls the instance | 
|  | 196 | method <tt>dealloc</tt> if the count reaches 0.  To simplify certain | 
|  | 197 | operations, there is also an <span class="term">autorelease | 
|  | 198 | pool</span>, a thread-local list of objects to call <tt>release</tt> | 
|  | 199 | on later; an object can be added to this pool by | 
|  | 200 | calling <tt>autorelease</tt> on it.</p> | 
|  | 201 |  | 
|  | 202 | <p>Block pointers may be converted to type <tt>id</tt>; block objects | 
|  | 203 | are laid out in a way that makes them compatible with Objective-C | 
|  | 204 | objects.  There is a builtin class that all block objects are | 
|  | 205 | considered to be objects of; this class implements <tt>retain</tt> by | 
|  | 206 | adjusting the reference count, not by calling <tt>Block_copy</tt>.</p> | 
|  | 207 |  | 
|  | 208 | </div> <!-- meta.background --> | 
|  | 209 |  | 
|  | 210 | </div> <!-- meta --> | 
|  | 211 |  | 
|  | 212 | <div id="general"> | 
|  | 213 | <h1>General</h1> | 
|  | 214 |  | 
|  | 215 | <p>Automatic Reference Counting implements automatic memory management | 
|  | 216 | for Objective-C objects and blocks, freeing the programmer from the | 
|  | 217 | need explicitly insert retains and releases.  It does not provide a | 
|  | 218 | cycle collector; users must explicitly manage lifetime instead.</p> | 
|  | 219 |  | 
|  | 220 | <p>ARC may be explicitly enabled with the compiler | 
|  | 221 | flag <tt>-fobjc-arc</tt>.  It may also be explicitly disabled with the | 
|  | 222 | compiler flag <tt>-fno-objc-arc</tt>.  The last of these two flags | 
|  | 223 | appearing on the compile line <q>wins</q>.</p> | 
|  | 224 |  | 
|  | 225 | <p>If ARC is enabled, <tt>__has_feature(objc_arc)</tt> will expand to | 
|  | 226 | 1 in the preprocessor.  For more information about <tt>__has_feature</tt>, | 
|  | 227 | see the <a href="LanguageExtensions.html#__has_feature_extension">language | 
|  | 228 | extensions</a> document.</p> | 
|  | 229 |  | 
|  | 230 | </div> | 
|  | 231 |  | 
|  | 232 | <div id="objects"> | 
|  | 233 | <h1>Retainable object pointers</h1> | 
|  | 234 |  | 
|  | 235 | <p>This section describes retainable object pointers, their basic | 
|  | 236 | operations, and the restrictions imposed on their use under ARC.  Note | 
|  | 237 | in particular that it covers the rules for pointer <em>values</em> | 
|  | 238 | (patterns of bits indicating the location of a pointed-to object), not | 
|  | 239 | pointer | 
|  | 240 | <em>objects</em> (locations in memory which store pointer values). | 
|  | 241 | The rules for objects are covered in the next section.</p> | 
|  | 242 |  | 
|  | 243 | <p>A <span class="term">retainable object pointer</span> | 
|  | 244 | (or <q>retainable pointer</q>) is a value of | 
|  | 245 | a <span class="term">retainable object pointer type</span> | 
|  | 246 | (<q>retainable type</q>).  There are three kinds of retainable object | 
|  | 247 | pointer types:</p> | 
|  | 248 | <ul> | 
|  | 249 | <li>block pointers (formed by applying the caret (<tt>^</tt>) | 
|  | 250 | declarator sigil to a function type)</li> | 
|  | 251 | <li>Objective-C object pointers (<tt>id</tt>, <tt>Class</tt>, <tt>NSFoo*</tt>, etc.)</li> | 
|  | 252 | <li>typedefs marked with <tt>__attribute__((NSObject))</tt></li> | 
|  | 253 | </ul> | 
|  | 254 |  | 
|  | 255 | <p>Other pointer types, such as <tt>int*</tt> and <tt>CFStringRef</tt>, | 
|  | 256 | are not subject to ARC's semantics and restrictions.</p> | 
|  | 257 |  | 
|  | 258 | <div class="rationale"> | 
|  | 259 |  | 
|  | 260 | <p>Rationale: We are not at liberty to require | 
|  | 261 | all code to be recompiled with ARC; therefore, ARC must interoperate | 
|  | 262 | with Objective-C code which manages retains and releases manually.  In | 
|  | 263 | general, there are three requirements in order for a | 
|  | 264 | compiler-supported reference-count system to provide reliable | 
|  | 265 | interoperation:</p> | 
|  | 266 |  | 
|  | 267 | <ul> | 
|  | 268 | <li>The type system must reliably identify which objects are to be | 
|  | 269 | managed.  An <tt>int*</tt> might be a pointer to a <tt>malloc</tt>'ed | 
|  | 270 | array, or it might be a interior pointer to such an array, or it might | 
|  | 271 | point to some field or local variable.  In contrast, values of the | 
|  | 272 | retainable object pointer types are never interior.</li> | 
|  | 273 | <li>The type system must reliably indicate how to | 
|  | 274 | manage objects of a type.  This usually means that the type must imply | 
|  | 275 | a procedure for incrementing and decrementing retain counts. | 
|  | 276 | Supporting single-ownership objects requires a lot more explicit | 
|  | 277 | mediation in the language.</li> | 
|  | 278 | <li>There must be reliable conventions for whether and | 
|  | 279 | when <q>ownership</q> is passed between caller and callee, for both | 
|  | 280 | arguments and return values.  Objective-C methods follow such a | 
|  | 281 | convention very reliably, at least for system libraries on Mac OS X, | 
|  | 282 | and functions always pass objects at +0.  The C-based APIs for Core | 
|  | 283 | Foundation objects, on the other hand, have much more varied transfer | 
|  | 284 | semantics.</li> | 
|  | 285 | </ul> | 
|  | 286 | </div> <!-- rationale --> | 
|  | 287 |  | 
|  | 288 | <p>The use of <tt>__attribute__((NSObject))</tt> typedefs is not | 
|  | 289 | recommended.  If it's absolutely necessary to use this attribute, be | 
|  | 290 | very explicit about using the typedef, and do not assume that it will | 
|  | 291 | be preserved by language features like <tt>__typeof</tt> and C++ | 
|  | 292 | template argument substitution.</p> | 
|  | 293 |  | 
|  | 294 | <div class="rationale"><p>Rationale: any compiler operation which | 
|  | 295 | incidentally strips type <q>sugar</q> from a type will yield a type | 
|  | 296 | without the attribute, which may result in unexpected | 
|  | 297 | behavior.</p></div> | 
|  | 298 |  | 
|  | 299 | <div id="objects.retains"> | 
|  | 300 | <h1>Retain count semantics</h1> | 
|  | 301 |  | 
|  | 302 | <p>A retainable object pointer is either a <span class="term">null | 
|  | 303 | pointer</span> or a pointer to a valid object.  Furthermore, if it has | 
|  | 304 | block pointer type and is not <tt>null</tt> then it must actually be a | 
|  | 305 | pointer to a block object, and if it has <tt>Class</tt> type (possibly | 
|  | 306 | protocol-qualified) then it must actually be a pointer to a class | 
|  | 307 | object.  Otherwise ARC does not enforce the Objective-C type system as | 
|  | 308 | long as the implementing methods follow the signature of the static | 
|  | 309 | type.  It is undefined behavior if ARC is exposed to an invalid | 
|  | 310 | pointer.</p> | 
|  | 311 |  | 
|  | 312 | <p>For ARC's purposes, a valid object is one with <q>well-behaved</q> | 
|  | 313 | retaining operations.  Specifically, the object must be laid out such | 
|  | 314 | that the Objective-C message send machinery can successfully send it | 
|  | 315 | the following messages:</p> | 
|  | 316 |  | 
|  | 317 | <ul> | 
|  | 318 | <li><tt>retain</tt>, taking no arguments and returning a pointer to | 
|  | 319 | the object.</li> | 
|  | 320 | <li><tt>release</tt>, taking no arguments and returning <tt>void</tt>.</li> | 
|  | 321 | <li><tt>autorelease</tt>, taking no arguments and returning a pointer | 
|  | 322 | to the object.</li> | 
|  | 323 | </ul> | 
|  | 324 |  | 
|  | 325 | <p>The behavior of these methods is constrained in the following ways. | 
|  | 326 | The term <span class="term">high-level semantics</span> is an | 
|  | 327 | intentionally vague term; the intent is that programmers must | 
|  | 328 | implement these methods in a way such that the compiler, modifying | 
|  | 329 | code in ways it deems safe according to these constraints, will not | 
|  | 330 | violate their requirements.  For example, if the user puts logging | 
|  | 331 | statements in <tt>retain</tt>, they should not be surprised if those | 
|  | 332 | statements are executed more or less often depending on optimization | 
|  | 333 | settings.  These constraints are not exhaustive of the optimization | 
|  | 334 | opportunities: values held in local variables are subject to | 
|  | 335 | additional restrictions, described later in this document.</p> | 
|  | 336 |  | 
|  | 337 | <p>It is undefined behavior if a computation history featuring a send | 
|  | 338 | of <tt>retain</tt> followed by a send of <tt>release</tt> to the same | 
|  | 339 | object, with no intervening <tt>release</tt> on that object, is not | 
|  | 340 | equivalent under the high-level semantics to a computation | 
|  | 341 | history in which these sends are removed.  Note that this implies that | 
|  | 342 | these methods may not raise exceptions.</p> | 
|  | 343 |  | 
|  | 344 | <p>It is undefined behavior if a computation history features any use | 
|  | 345 | whatsoever of an object following the completion of a send | 
|  | 346 | of <tt>release</tt> that is not preceded by a send of <tt>retain</tt> | 
|  | 347 | to the same object.</p> | 
|  | 348 |  | 
|  | 349 | <p>The behavior of <tt>autorelease</tt> must be equivalent to sending | 
|  | 350 | <tt>release</tt> when one of the autorelease pools currently in scope | 
|  | 351 | is popped.  It may not throw an exception.</p> | 
|  | 352 |  | 
|  | 353 | <p>When the semantics call for performing one of these operations on a | 
|  | 354 | retainable object pointer, if that pointer is <tt>null</tt> then the | 
|  | 355 | effect is a no-op.</p> | 
|  | 356 |  | 
|  | 357 | <p>All of the semantics described in this document are subject to | 
|  | 358 | additional <a href="#optimization">optimization rules</a> which permit | 
|  | 359 | the removal or optimization of operations based on local knowledge of | 
|  | 360 | data flow.  The semantics describe the high-level behaviors that the | 
|  | 361 | compiler implements, not an exact sequence of operations that a | 
|  | 362 | program will be compiled into.</p> | 
|  | 363 |  | 
|  | 364 | </div> <!-- objects.retains --> | 
|  | 365 |  | 
|  | 366 | <div id="objects.operands"> | 
|  | 367 | <h1>Retainable object pointers as operands and arguments</h1> | 
|  | 368 |  | 
|  | 369 | <p>In general, ARC does not perform retain or release operations when | 
|  | 370 | simply using a retainable object pointer as an operand within an | 
|  | 371 | expression.  This includes:</p> | 
|  | 372 | <ul> | 
|  | 373 | <li>loading a retainable pointer from an object with non-weak | 
|  | 374 | <a href="#ownership">ownership</a>,</li> | 
|  | 375 | <li>passing a retainable pointer as an argument to a function or | 
|  | 376 | method, and</li> | 
|  | 377 | <li>receiving a retainable pointer as the result of a function or | 
|  | 378 | method call.</li> | 
|  | 379 | </ul> | 
|  | 380 |  | 
|  | 381 | <div class="rationale"><p>Rationale: while this might seem | 
|  | 382 | uncontroversial, it is actually unsafe when multiple expressions are | 
|  | 383 | evaluated in <q>parallel</q>, as with binary operators and calls, | 
|  | 384 | because (for example) one expression might load from an object while | 
|  | 385 | another writes to it.  However, C and C++ already call this undefined | 
|  | 386 | behavior because the evaluations are unsequenced, and ARC simply | 
|  | 387 | exploits that here to avoid needing to retain arguments across a large | 
|  | 388 | number of calls.</p></div> | 
|  | 389 |  | 
|  | 390 | <p>The remainder of this section describes exceptions to these rules, | 
|  | 391 | how those exceptions are detected, and what those exceptions imply | 
|  | 392 | semantically.</p> | 
|  | 393 |  | 
|  | 394 | <div id="objects.operands.consumed"> | 
|  | 395 | <h1>Consumed parameters</h1> | 
|  | 396 |  | 
|  | 397 | <p>A function or method parameter of retainable object pointer type | 
|  | 398 | may be marked as <span class="term">consumed</span>, signifying that | 
|  | 399 | the callee expects to take ownership of a +1 retain count.  This is | 
|  | 400 | done by adding the <tt>ns_consumed</tt> attribute to the parameter | 
|  | 401 | declaration, like so:</p> | 
|  | 402 |  | 
|  | 403 | <pre>void foo(__attribute((ns_consumed)) id x); | 
|  | 404 | - (void) foo: (id) __attribute((ns_consumed)) x;</pre> | 
|  | 405 |  | 
|  | 406 | <p>This attribute is part of the type of the function or method, not | 
|  | 407 | the type of the parameter.  It controls only how the argument is | 
|  | 408 | passed and received.</p> | 
|  | 409 |  | 
|  | 410 | <p>When passing such an argument, ARC retains the argument prior to | 
|  | 411 | making the call.</p> | 
|  | 412 |  | 
|  | 413 | <p>When receiving such an argument, ARC releases the argument at the | 
|  | 414 | end of the function, subject to the usual optimizations for local | 
|  | 415 | values.</p> | 
|  | 416 |  | 
|  | 417 | <div class="rationale"><p>Rationale: this formalizes direct transfers | 
|  | 418 | of ownership from a caller to a callee.  The most common scenario here | 
|  | 419 | is passing the <tt>self</tt> parameter to <tt>init</tt>, but it is | 
|  | 420 | useful to generalize.  Typically, local optimization will remove any | 
|  | 421 | extra retains and releases: on the caller side the retain will be | 
|  | 422 | merged with a +1 source, and on the callee side the release will be | 
|  | 423 | rolled into the initialization of the parameter.</p></div> | 
|  | 424 |  | 
|  | 425 | <p>The implicit <tt>self</tt> parameter of a method may be marked as | 
|  | 426 | consumed by adding <tt>__attribute__((ns_consumes_self))</tt> to the | 
|  | 427 | method declaration.  Methods in | 
|  | 428 | the <tt>init</tt> <a href="#family">family</a> are implicitly | 
|  | 429 | marked <tt>__attribute__((ns_consumes_self))</tt>.</p> | 
|  | 430 |  | 
|  | 431 | <p>It is undefined behavior if an Objective-C message send of a method | 
|  | 432 | with <tt>ns_consumed</tt> parameters (other than self) is made to a | 
|  | 433 | null pointer.</p> | 
|  | 434 |  | 
|  | 435 | <div class="rationale"><p>Rationale: in fact, it's probably a | 
|  | 436 | guaranteed leak.</p></div> | 
|  | 437 |  | 
|  | 438 | </div> | 
|  | 439 |  | 
|  | 440 | <div id="objects.operands.retained-returns"> | 
|  | 441 | <h1>Retained return values</h1> | 
|  | 442 |  | 
|  | 443 | <p>A function or method which returns a retainable object pointer type | 
|  | 444 | may be marked as returning a retained value, signifying that the | 
|  | 445 | caller expects to take ownership of a +1 retain count.  This is done | 
|  | 446 | by adding the <tt>ns_returns_retained</tt> attribute to the function or | 
|  | 447 | method declaration, like so:</p> | 
|  | 448 |  | 
|  | 449 | <pre>id foo(void) __attribute((ns_returns_retained)); | 
|  | 450 | - (id) foo __attribute((ns_returns_retained));</pre> | 
|  | 451 |  | 
|  | 452 | <p>This attribute is part of the type of the function or method.</p> | 
|  | 453 |  | 
|  | 454 | <p>When returning from such a function or method, ARC retains the | 
|  | 455 | value at the point of evaluation of the return statement, before | 
|  | 456 | leaving all local scopes.</p> | 
|  | 457 |  | 
|  | 458 | <p>When receiving a return result from such a function or method, ARC | 
|  | 459 | releases the value at the end of the full-expression it is contained | 
|  | 460 | within, subject to the usual optimizations for local values.</p> | 
|  | 461 |  | 
|  | 462 | <div class="rationale"><p>Rationale: this formalizes direct transfers of | 
|  | 463 | ownership from a callee to a caller.  The most common scenario this | 
|  | 464 | models is the retained return from <tt>init</tt>, <tt>alloc</tt>, | 
|  | 465 | <tt>new</tt>, and <tt>copy</tt> methods, but there are other cases in | 
|  | 466 | the frameworks.  After optimization there are typically no extra | 
|  | 467 | retains and releases required.</p></div> | 
|  | 468 |  | 
|  | 469 | <p>Methods in | 
|  | 470 | the <tt>alloc</tt>, <tt>copy</tt>, <tt>init</tt>, <tt>mutableCopy</tt>, | 
|  | 471 | and <tt>new</tt> <a href="#family">families</a> are implicitly marked | 
|  | 472 | <tt>__attribute__((ns_returns_retained))</tt>.  This may be suppressed | 
|  | 473 | by explicitly marking the | 
|  | 474 | method <tt>__attribute__((ns_returns_not_retained))</tt>.</p> | 
|  | 475 | </div> | 
|  | 476 |  | 
|  | 477 | <div id="objects.operands.other-returns"> | 
|  | 478 | <h1>Unretained return values</h1> | 
|  | 479 |  | 
|  | 480 | <p>A method or function which returns a retainable object type but | 
|  | 481 | does not return a retained value must ensure that the object is | 
|  | 482 | still valid across the return boundary.</p> | 
|  | 483 |  | 
|  | 484 | <p>When returning from such a function or method, ARC retains the | 
|  | 485 | value at the point of evaluation of the return statement, then leaves | 
|  | 486 | all local scopes, and then balances out the retain while ensuring that | 
|  | 487 | the value lives across the call boundary.  In the worst case, this may | 
|  | 488 | involve an <tt>autorelease</tt>, but callers must not assume that the | 
|  | 489 | value is actually in the autorelease pool.</p> | 
|  | 490 |  | 
|  | 491 | <p>ARC performs no extra mandatory work on the caller side, although | 
|  | 492 | it may elect to do something to shorten the lifetime of the returned | 
|  | 493 | value.</p> | 
|  | 494 |  | 
|  | 495 | <div class="rationale"><p>Rationale: it is common in non-ARC code to not | 
|  | 496 | return an autoreleased value; therefore the convention does not force | 
|  | 497 | either path.  It is convenient to not be required to do unnecessary | 
|  | 498 | retains and autoreleases; this permits optimizations such as eliding | 
|  | 499 | retain/autoreleases when it can be shown that the original pointer | 
|  | 500 | will still be valid at the point of return.</p></div> | 
|  | 501 |  | 
|  | 502 | <p>A method or function may be marked | 
|  | 503 | with <tt>__attribute__((ns_returns_autoreleased))</tt> to indicate | 
|  | 504 | that it returns a pointer which is guaranteed to be valid at least as | 
|  | 505 | long as the innermost autorelease pool.  There are no additional | 
|  | 506 | semantics enforced in the definition of such a method; it merely | 
|  | 507 | enables optimizations in callers.</p> | 
|  | 508 | </div> | 
|  | 509 |  | 
|  | 510 | <div id="objects.operands.casts"> | 
|  | 511 | <h1>Bridged casts</h1> | 
|  | 512 |  | 
|  | 513 | <p>A <span class="term">bridged cast</span> is a C-style cast | 
|  | 514 | annotated with one of three keywords:</p> | 
|  | 515 |  | 
|  | 516 | <ul> | 
|  | 517 | <li><tt>(__bridge T) op</tt> casts the operand to the destination | 
|  | 518 | type <tt>T</tt>.  If <tt>T</tt> is a retainable object pointer type, | 
|  | 519 | then <tt>op</tt> must have a non-retainable pointer type. | 
|  | 520 | If <tt>T</tt> is a non-retainable pointer type, then <tt>op</tt> must | 
|  | 521 | have a retainable object pointer type.  Otherwise the cast is | 
|  | 522 | ill-formed.  There is no transfer of ownership, and ARC inserts | 
|  | 523 | no retain operations.</li> | 
|  | 524 |  | 
|  | 525 | <li><tt>(__bridge_retained T) op</tt> casts the operand, which must | 
|  | 526 | have retainable object pointer type, to the destination type, which | 
|  | 527 | must be a non-retainable pointer type.  ARC retains the value, subject | 
|  | 528 | to the usual optimizations on local values, and the recipient is | 
|  | 529 | responsible for balancing that +1.</li> | 
|  | 530 |  | 
|  | 531 | <li><tt>(__bridge_transfer T) op</tt> casts the operand, which must | 
|  | 532 | have non-retainable pointer type, to the destination type, which must | 
|  | 533 | be a retainable object pointer type.  ARC will release the value at | 
|  | 534 | the end of the enclosing full-expression, subject to the usual | 
|  | 535 | optimizations on local values.</li> | 
|  | 536 | </ul> | 
|  | 537 |  | 
|  | 538 | <p>These casts are required in order to transfer objects in and out of | 
|  | 539 | ARC control; see the rationale in the section | 
|  | 540 | on <a href="#objects.restrictions.conversion">conversion of retainable | 
|  | 541 | object pointers</a>.</p> | 
|  | 542 |  | 
|  | 543 | <p>Using a <tt>__bridge_retained</tt> or <tt>__bridge_transfer</tt> | 
|  | 544 | cast purely to convince ARC to emit an unbalanced retain or release, | 
|  | 545 | respectively, is poor form.</p> | 
|  | 546 |  | 
|  | 547 | </div> | 
|  | 548 |  | 
|  | 549 | </div> | 
|  | 550 |  | 
|  | 551 | <div id="objects.restrictions"> | 
|  | 552 | <h1>Restrictions</h1> | 
|  | 553 |  | 
|  | 554 | <div id="objects.restrictions.conversion"> | 
|  | 555 | <h1>Conversion of retainable object pointers</h1> | 
|  | 556 |  | 
|  | 557 | <p>In general, a program which attempts to implicitly or explicitly | 
|  | 558 | convert a value of retainable object pointer type to any | 
|  | 559 | non-retainable type, or vice-versa, is ill-formed.  For example, an | 
|  | 560 | Objective-C object pointer shall not be converted to <tt>intptr_t</tt> | 
|  | 561 | or <tt>void*</tt>.  The <a href="#objects.operands.casts">bridged | 
|  | 562 | casts</a> may be used to perform these conversions where | 
|  | 563 | necessary.</p> | 
|  | 564 |  | 
|  | 565 | <div class="rationale"><p>Rationale: we cannot ensure the correct | 
|  | 566 | management of the lifetime of objects if they may be freely passed | 
|  | 567 | around as unmanaged types.  The bridged casts are provided so that the | 
|  | 568 | programmer may explicitly describe whether the cast transfers control | 
|  | 569 | into or out of ARC.</p></div> | 
|  | 570 | </div> | 
|  | 571 |  | 
|  | 572 | <p>An unbridged cast to a retainable object pointer type of the return | 
|  | 573 | value of a Objective-C message send which yields a non-retainable | 
|  | 574 | pointer is treated as a <tt>__bridge_transfer</tt> cast | 
|  | 575 | if:</p> | 
|  | 576 |  | 
|  | 577 | <ul> | 
|  | 578 | <li>the method has the <tt>cf_returns_retained</tt> attribute, or if | 
|  | 579 | not that,</li> | 
|  | 580 | <li>the method does not have the <tt>cf_returns_not_retained</tt> | 
|  | 581 | attribute and</li> | 
|  | 582 | <li>the method's <a href="#family">selector family</a> would imply | 
|  | 583 | the <tt>ns_returns_retained</tt> attribute on a method which returned | 
|  | 584 | a retainable object pointer type.</li> | 
|  | 585 | </ul> | 
|  | 586 |  | 
|  | 587 | <p>Otherwise the cast is treated as a <tt>__bridge</tt> cast.</p> | 
|  | 588 |  | 
|  | 589 | </div> | 
|  | 590 |  | 
|  | 591 | </div> | 
|  | 592 |  | 
|  | 593 | <div id="ownership"> | 
|  | 594 | <h1>Ownership qualification</h1> | 
|  | 595 |  | 
|  | 596 | <p>This section describes the behavior of <em>objects</em> of | 
|  | 597 | retainable object pointer type; that is, locations in memory which | 
|  | 598 | store retainable object pointers.</p> | 
|  | 599 |  | 
|  | 600 | <p>A type is a <span class="term">retainable object owner type</span> | 
|  | 601 | if it is a retainable object pointer type or an array type whose | 
|  | 602 | element type is a retainable object owner type.</p> | 
|  | 603 |  | 
|  | 604 | <p>An <span class="term">ownership qualifier</span> is a type | 
| Douglas Gregor | 4020cae | 2011-06-17 23:16:24 +0000 | [diff] [blame] | 605 | qualifier which applies only to retainable object owner types. An array type is | 
|  | 606 | ownership-qualified according to its element type, and adding an ownership | 
|  | 607 | qualifier to an array type so qualifies its element type.</p> | 
|  | 608 |  | 
|  | 609 | <p>A program is ill-formed if it attempts to apply an ownership qualifier | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 610 | to a type which is already ownership-qualified, even if it is the same | 
| Douglas Gregor | 4020cae | 2011-06-17 23:16:24 +0000 | [diff] [blame] | 611 | qualifier. There is a single exception to this rule: an ownership qualifier | 
|  | 612 | may be applied to a substituted template type parameter, which overrides the | 
|  | 613 | ownership qualifier provided by the template argument.</p> | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 614 |  | 
|  | 615 | <p>Except as described under | 
|  | 616 | the <a href="#ownership.inference">inference rules</a>, a program is | 
|  | 617 | ill-formed if it attempts to form a pointer or reference type to a | 
|  | 618 | retainable object owner type which lacks an ownership qualifier.</p> | 
|  | 619 |  | 
|  | 620 | <div class="rationale"><p>Rationale: these rules, together with the | 
|  | 621 | inference rules, ensure that all objects and lvalues of retainable | 
| Douglas Gregor | 4020cae | 2011-06-17 23:16:24 +0000 | [diff] [blame] | 622 | object pointer type have an ownership qualifier. The ability to override an ownership qualifier during template substitution is required to counteract the <a href="#ownership.inference.template_arguments">inference of <tt>__strong</tt> for template type arguments</a>. </p></div> | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 623 |  | 
|  | 624 | <p>There are four ownership qualifiers:</p> | 
|  | 625 |  | 
|  | 626 | <ul> | 
|  | 627 | <li><tt>__autoreleasing</tt></li> | 
|  | 628 | <li><tt>__strong</tt></li> | 
|  | 629 | <li><tt>__unsafe_unretained</tt></li> | 
|  | 630 | <li><tt>__weak</tt></li> | 
|  | 631 | </ul> | 
|  | 632 |  | 
|  | 633 | <p>A type is <span class="term">nontrivially ownership-qualified</span> | 
|  | 634 | if it is qualified with <tt>__autoreleasing</tt>, <tt>__strong</tt>, or | 
|  | 635 | <tt>__weak</tt>.</p> | 
|  | 636 |  | 
|  | 637 | <div id="ownership.spelling"> | 
|  | 638 | <h1>Spelling</h1> | 
|  | 639 |  | 
|  | 640 | <p>The names of the ownership qualifiers are reserved for the | 
|  | 641 | implementation.  A program may not assume that they are or are not | 
|  | 642 | implemented with macros, or what those macros expand to.</p> | 
|  | 643 |  | 
|  | 644 | <p>An ownership qualifier may be written anywhere that any other type | 
|  | 645 | qualifier may be written.</p> | 
|  | 646 |  | 
|  | 647 | <p>If an ownership qualifier appears in | 
|  | 648 | the <i>declaration-specifiers</i>, the following rules apply:</p> | 
|  | 649 |  | 
|  | 650 | <ul> | 
|  | 651 | <li>if the type specifier is a retainable object owner type, the | 
|  | 652 | qualifier applies to that type;</li> | 
|  | 653 | <li>if the outermost non-array part of the declarator is a pointer or | 
|  | 654 | block pointer, the qualifier applies to that type;</li> | 
|  | 655 | <li>otherwise the program is ill-formed.</li> | 
|  | 656 | </ul> | 
|  | 657 |  | 
|  | 658 | <p>If an ownership qualifier appears on the declarator name, or on the | 
|  | 659 | declared object, it is applied to outermost pointer or block-pointer | 
|  | 660 | type.</p> | 
|  | 661 |  | 
|  | 662 | <p>If an ownership qualifier appears anywhere else in a declarator, it | 
|  | 663 | applies to the type there.</p> | 
|  | 664 |  | 
|  | 665 | </div> <!-- ownership.spelling --> | 
|  | 666 |  | 
|  | 667 | <div id="ownership.semantics"> | 
|  | 668 | <h1>Semantics</h1> | 
|  | 669 |  | 
|  | 670 | <p>There are five <span class="term">managed operations</span> which | 
|  | 671 | may be performed on an object of retainable object pointer type.  Each | 
|  | 672 | qualifier specifies different semantics for each of these operations. | 
|  | 673 | It is still undefined behavior to access an object outside of its | 
|  | 674 | lifetime.</p> | 
|  | 675 |  | 
|  | 676 | <p>A load or store with <q>primitive semantics</q> has the same | 
|  | 677 | semantics as the respective operation would have on an <tt>void*</tt> | 
|  | 678 | lvalue with the same alignment and non-ownership qualification.</p> | 
|  | 679 |  | 
|  | 680 | <p><span class="term">Reading</span> occurs when performing a | 
|  | 681 | lvalue-to-rvalue conversion on an object lvalue. | 
|  | 682 |  | 
|  | 683 | <ul> | 
|  | 684 | <li>For <tt>__weak</tt> objects, the current pointee is retained and | 
|  | 685 | then released at the end of the current full-expression.  This must | 
|  | 686 | execute atomically with respect to assignments and to the final | 
|  | 687 | release of the pointee.</li> | 
|  | 688 | <li>For all other objects, the lvalue is loaded with primitive | 
|  | 689 | semantics.</li> | 
|  | 690 | </ul> | 
|  | 691 | </p> | 
|  | 692 |  | 
|  | 693 | <p><span class="term">Assignment</span> occurs when evaluating | 
|  | 694 | an assignment operator.  The semantics vary based on the qualification: | 
|  | 695 | <ul> | 
|  | 696 | <li>For <tt>__strong</tt> objects, the new pointee is first retained; | 
|  | 697 | second, the lvalue is loaded with primitive semantics; third, the new | 
|  | 698 | pointee is stored into the lvalue with primitive semantics; and | 
|  | 699 | finally, the old pointee is released.  This is not performed | 
|  | 700 | atomically; external synchronization must be used to make this safe in | 
|  | 701 | the face of concurrent loads and stores.</li> | 
|  | 702 | <li>For <tt>__weak</tt> objects, the lvalue is updated to point to the | 
|  | 703 | new pointee, unless that object is currently undergoing deallocation, | 
|  | 704 | in which case it the lvalue is updated to a null pointer.  This must | 
|  | 705 | execute atomically with respect to other assignments to the object, to | 
|  | 706 | reads from the object, and to the final release of the new pointed-to | 
|  | 707 | value.</li> | 
|  | 708 | <li>For <tt>__unsafe_unretained</tt> objects, the new pointee is | 
|  | 709 | stored into the lvalue using primitive semantics.</li> | 
|  | 710 | <li>For <tt>__autoreleasing</tt> objects, the new pointee is retained, | 
|  | 711 | autoreleased, and stored into the lvalue using primitive semantics.</li> | 
|  | 712 | </ul> | 
|  | 713 | </p> | 
|  | 714 |  | 
|  | 715 | <p><span class="term">Initialization</span> occurs when an object's | 
|  | 716 | lifetime begins, which depends on its storage duration. | 
|  | 717 | Initialization proceeds in two stages: | 
|  | 718 | <ol> | 
|  | 719 | <li>First, a null pointer is stored into the lvalue using primitive | 
|  | 720 | semantics.  This step is skipped if the object | 
|  | 721 | is <tt>__unsafe_unretained</tt>.</li> | 
|  | 722 | <li>Second, if the object has an initializer, that expression is | 
|  | 723 | evaluated and then assigned into the object using the usual assignment | 
|  | 724 | semantics.</li> | 
|  | 725 | </ol> | 
|  | 726 | </p> | 
|  | 727 |  | 
|  | 728 | <p><span class="term">Destruction</span> occurs when an object's | 
|  | 729 | lifetime ends.  In all cases it is semantically equivalent to | 
|  | 730 | assigning a null pointer to the object, with the proviso that of | 
|  | 731 | course the object cannot be legally read after the object's lifetime | 
|  | 732 | ends.</p> | 
|  | 733 |  | 
|  | 734 | <p><span class="term">Moving</span> occurs in specific situations | 
|  | 735 | where an lvalue is <q>moved from</q>, meaning that its current pointee | 
|  | 736 | will be used but the object may be left in a different (but still | 
|  | 737 | valid) state.  This arises with <tt>__block</tt> variables and rvalue | 
|  | 738 | references in C++. For <tt>__strong</tt> lvalues, moving is equivalent | 
|  | 739 | to loading the lvalue with primitive semantics, writing a null pointer | 
|  | 740 | to it with primitive semantics, and then releasing the result of the | 
|  | 741 | load at the end of the current full-expression.  For all other | 
|  | 742 | lvalues, moving is equivalent to reading the object.</p> | 
|  | 743 |  | 
|  | 744 | </div> <!-- ownership.semantics --> | 
|  | 745 |  | 
|  | 746 | <div id="ownership.restrictions"> | 
|  | 747 | <h1>Restrictions</h1> | 
|  | 748 |  | 
|  | 749 | <div id="ownership.restrictions.autoreleasing"> | 
|  | 750 | <h1>Storage duration of<tt> __autoreleasing</tt> objects</h1> | 
|  | 751 |  | 
|  | 752 | <p>A program is ill-formed if it declares an <tt>__autoreleasing</tt> | 
|  | 753 | object of non-automatic storage duration.</p> | 
|  | 754 |  | 
|  | 755 | <div class="rationale"><p>Rationale: autorelease pools are tied to the | 
|  | 756 | current thread and scope by their nature.  While it is possible to | 
|  | 757 | have temporary objects whose instance variables are filled with | 
|  | 758 | autoreleased objects, there is no way that ARC can provide any sort of | 
|  | 759 | safety guarantee there.</p></div> | 
|  | 760 |  | 
|  | 761 | <p>It is undefined behavior if a non-null pointer is assigned to | 
|  | 762 | an <tt>__autoreleasing</tt> object while an autorelease pool is in | 
|  | 763 | scope and then that object is read after the autorelease pool's scope | 
|  | 764 | is left.</p> | 
|  | 765 |  | 
|  | 766 | </div> | 
|  | 767 |  | 
|  | 768 | <div id="ownership.restrictions.conversion.indirect"> | 
|  | 769 | <h1>Conversion of pointers to ownership-qualified types</h1> | 
|  | 770 |  | 
|  | 771 | <p>A program is ill-formed if an expression of type <tt>T*</tt> is | 
|  | 772 | converted, explicitly or implicitly, to the type <tt>U*</tt>, | 
|  | 773 | where <tt>T</tt> and <tt>U</tt> have different ownership | 
|  | 774 | qualification, unless: | 
|  | 775 | <ul> | 
|  | 776 | <li><tt>T</tt> is qualified with <tt>__strong</tt>, | 
|  | 777 | <tt>__autoreleasing</tt>, or <tt>__unsafe_unretained</tt>, and | 
|  | 778 | <tt>U</tt> is qualified with both <tt>const</tt> and | 
|  | 779 | <tt>__unsafe_unretained</tt>; or</li> | 
|  | 780 | <li>either <tt>T</tt> or <tt>U</tt> is <tt>cv void</tt>, where | 
|  | 781 | <tt>cv</tt> is an optional sequence of non-ownership qualifiers; or</li> | 
|  | 782 | <li>the conversion is requested with a <tt>reinterpret_cast</tt> in | 
|  | 783 | Objective-C++; or</li> | 
|  | 784 | <li>the conversion is a | 
|  | 785 | well-formed <a href="#ownership.restrictions.pass_by_writeback">pass-by-writeback</a>.</li> | 
|  | 786 | </ul> | 
|  | 787 | </p> | 
|  | 788 |  | 
|  | 789 | <p>The analogous rule applies to <tt>T&</tt> and <tt>U&</tt> in | 
|  | 790 | Objective-C++.</p> | 
|  | 791 |  | 
|  | 792 | <div class="rationale"><p>Rationale: these rules provide a reasonable | 
|  | 793 | level of type-safety for indirect pointers, as long as the underlying | 
|  | 794 | memory is not deallocated.  The conversion to <tt>const | 
|  | 795 | __unsafe_unretained</tt> is permitted because the semantics of reads | 
|  | 796 | are equivalent across all these ownership semantics, and that's a very | 
|  | 797 | useful and common pattern.  The interconversion with <tt>void*</tt> is | 
|  | 798 | useful for allocating memory or otherwise escaping the type system, | 
|  | 799 | but use it carefully.  <tt>reinterpret_cast</tt> is considered to be | 
|  | 800 | an obvious enough sign of taking responsibility for any | 
|  | 801 | problems.</p></div> | 
|  | 802 |  | 
|  | 803 | <p>It is undefined behavior to access an ownership-qualified object | 
|  | 804 | through an lvalue of a differently-qualified type, except that any | 
|  | 805 | non-<tt>__weak</tt> object may be read through | 
|  | 806 | an <tt>__unsafe_unretained</tt> lvalue.</p> | 
|  | 807 |  | 
|  | 808 | <p>It is undefined behavior if a managed operation is performed on | 
|  | 809 | a <tt>__strong</tt> or <tt>__weak</tt> object without a guarantee that | 
|  | 810 | it contains a primitive zero bit-pattern, or if the storage for such | 
|  | 811 | an object is freed or reused without the object being first assigned a | 
|  | 812 | null pointer.</p> | 
|  | 813 |  | 
|  | 814 | <div class="rationale"><p>Rationale: ARC cannot differentiate between | 
|  | 815 | an assignment operator which is intended to <q>initialize</q> dynamic | 
|  | 816 | memory and one which is intended to potentially replace a value. | 
|  | 817 | Therefore the object's pointer must be valid before letting ARC at it. | 
|  | 818 | Similarly, C and Objective-C do not provide any language hooks for | 
|  | 819 | destroying objects held in dynamic memory, so it is the programmer's | 
|  | 820 | responsibility to avoid leaks (<tt>__strong</tt> objects) and | 
|  | 821 | consistency errors (<tt>__weak</tt> objects).</p> | 
|  | 822 |  | 
|  | 823 | <p>These requirements are followed automatically in Objective-C++ when | 
|  | 824 | creating objects of retainable object owner type with <tt>new</tt> | 
|  | 825 | or <tt>new[]</tt> and destroying them with <tt>delete</tt>, | 
|  | 826 | <tt>delete[]</tt>, or a pseudo-destructor expression.  Note that | 
|  | 827 | arrays of nontrivially-ownership-qualified type are not ABI compatible | 
|  | 828 | with non-ARC code because the element type is non-POD: such arrays | 
|  | 829 | that are <tt>new[]</tt>'d in ARC translation units cannot | 
|  | 830 | be <tt>delete[]</tt>'d in non-ARC translation units and | 
|  | 831 | vice-versa.</p></div> | 
|  | 832 |  | 
|  | 833 | </div> | 
|  | 834 |  | 
|  | 835 | <div id="ownership.restrictions.pass_by_writeback"> | 
|  | 836 | <h1>Passing to an out parameter by writeback</h1> | 
|  | 837 |  | 
|  | 838 | <p>If the argument passed to a parameter of type | 
|  | 839 | <tt>T __autoreleasing *</tt> has type <tt>U oq *</tt>, | 
|  | 840 | where <tt>oq</tt> is an ownership qualifier, then the argument is a | 
|  | 841 | candidate for <span class="term">pass-by-writeback</span> if:</p> | 
|  | 842 |  | 
|  | 843 | <ul> | 
|  | 844 | <li><tt>oq</tt> is <tt>__strong</tt> or <tt>__weak</tt>, and | 
|  | 845 | <li>it would be legal to initialize a <tt>T __strong *</tt> with | 
|  | 846 | a <tt>U __strong *</tt>.</li> | 
|  | 847 | </ul> | 
|  | 848 |  | 
|  | 849 | <p>For purposes of overload resolution, an implicit conversion | 
|  | 850 | sequence requiring a pass-by-writeback is always worse than an | 
|  | 851 | implicit conversion sequence not requiring a pass-by-writeback.</p> | 
|  | 852 |  | 
|  | 853 | <p>The pass-by-writeback is ill-formed if the argument expression does | 
|  | 854 | not have a legal form:</p> | 
|  | 855 |  | 
|  | 856 | <ul> | 
|  | 857 | <li><tt>&var</tt>, where <tt>var</tt> is a scalar variable of | 
|  | 858 | automatic storage duration with retainable object pointer type</li> | 
|  | 859 | <li>a conditional expression where the second and third operands are | 
|  | 860 | both legal forms</li> | 
|  | 861 | <li>a cast whose operand is a legal form</li> | 
|  | 862 | <li>a null pointer constant</li> | 
|  | 863 | </ul> | 
|  | 864 |  | 
|  | 865 | <div class="rationale"><p>Rationale: the restriction in the form of | 
|  | 866 | the argument serves two purposes.  First, it makes it impossible to | 
|  | 867 | pass the address of an array to the argument, which serves to protect | 
|  | 868 | against an otherwise serious risk of mis-inferring an <q>array</q> | 
|  | 869 | argument as an out-parameter.  Second, it makes it much less likely | 
|  | 870 | that the user will see confusing aliasing problems due to the | 
|  | 871 | implementation, below, where their store to the writeback temporary is | 
|  | 872 | not immediately seen in the original argument variable.</p></div> | 
|  | 873 |  | 
|  | 874 | <p>A pass-by-writeback is evaluated as follows: | 
|  | 875 | <ol> | 
|  | 876 | <li>The argument is evaluated to yield a pointer <tt>p</tt> of | 
|  | 877 | type <tt>U oq *</tt>.</li> | 
|  | 878 | <li>If <tt>p</tt> is a null pointer, then a null pointer is passed as | 
|  | 879 | the argument, and no further work is required for the pass-by-writeback.</li> | 
|  | 880 | <li>Otherwise, a temporary of type <tt>T __autoreleasing</tt> is | 
|  | 881 | created and initialized to a null pointer.</li> | 
|  | 882 | <li>If the argument is not an Objective-C method parameter marked | 
|  | 883 | <tt>out</tt>, then <tt>*p</tt> is read, and the result is written | 
|  | 884 | into the temporary with primitive semantics.</li> | 
|  | 885 | <li>The address of the temporary is passed as the argument to the | 
|  | 886 | actual call.</li> | 
|  | 887 | <li>After the call completes, the temporary is loaded with primitive | 
|  | 888 | semantics, and that value is assigned into <tt>*p</tt>.</li> | 
|  | 889 | </ol></p> | 
|  | 890 |  | 
|  | 891 | <div class="rationale"><p>Rationale: this is all admittedly | 
|  | 892 | convoluted.  In an ideal world, we would see that a local variable is | 
|  | 893 | being passed to an out-parameter and retroactively modify its type to | 
|  | 894 | be <tt>__autoreleasing</tt> rather than <tt>__strong</tt>.  This would | 
|  | 895 | be remarkably difficult and not always well-founded under the C type | 
|  | 896 | system.  However, it was judged unacceptably invasive to require | 
|  | 897 | programmers to write <tt>__autoreleasing</tt> on all the variables | 
|  | 898 | they intend to use for out-parameters.  This was the least bad | 
|  | 899 | solution.</p></div> | 
|  | 900 |  | 
|  | 901 | </div> | 
|  | 902 |  | 
|  | 903 | <div id="ownership.restrictions.records"> | 
|  | 904 | <h1>Ownership-qualified fields of structs and unions</h1> | 
|  | 905 |  | 
|  | 906 | <p>A program is ill-formed if it declares a member of a C struct or | 
|  | 907 | union to have a nontrivially ownership-qualified type.</p> | 
|  | 908 |  | 
|  | 909 | <div class="rationale"><p>Rationale: the resulting type would be | 
|  | 910 | non-POD in the C++ sense, but C does not give us very good language | 
|  | 911 | tools for managing the lifetime of aggregates, so it is more | 
|  | 912 | convenient to simply forbid them.  It is still possible to manage this | 
|  | 913 | with a <tt>void*</tt> or an <tt>__unsafe_unretained</tt> | 
|  | 914 | object.</p></div> | 
|  | 915 |  | 
|  | 916 | <p>This restriction does not apply in Objective-C++.  However, | 
|  | 917 | nontrivally ownership-qualified types are considered non-POD: in C++0x | 
|  | 918 | terms, they are not trivially default constructible, copy | 
|  | 919 | constructible, move constructible, copy assignable, move assignable, | 
|  | 920 | or destructible.  It is a violation of C++ One Definition Rule to use | 
|  | 921 | a class outside of ARC that, under ARC, would have an | 
|  | 922 | ownership-qualified member.</p> | 
|  | 923 |  | 
|  | 924 | <div class="rationale"><p>Rationale: unlike in C, we can express all | 
|  | 925 | the necessary ARC semantics for ownership-qualified subobjects as | 
|  | 926 | suboperations of the (default) special member functions for the class. | 
|  | 927 | These functions then become non-trivial.  This has the non-obvious | 
|  | 928 | repercussion that the class will have a non-trivial copy constructor | 
|  | 929 | and non-trivial destructor; if it wouldn't outside of ARC, this means | 
|  | 930 | that objects of the type will be passed and returned in an | 
|  | 931 | ABI-incompatible manner.</p></div> | 
|  | 932 |  | 
|  | 933 | </div> | 
|  | 934 |  | 
|  | 935 | </div> | 
|  | 936 |  | 
|  | 937 | <div id="ownership.inference"> | 
|  | 938 | <h1>Ownership inference</h1> | 
|  | 939 |  | 
|  | 940 | <div id="ownership.inference.variables"> | 
|  | 941 | <h1>Objects</h1> | 
|  | 942 |  | 
|  | 943 | <p>If an object is declared with retainable object owner type, but | 
|  | 944 | without an explicit ownership qualifier, its type is implicitly | 
|  | 945 | adjusted to have <tt>__strong</tt> qualification.</p> | 
|  | 946 |  | 
|  | 947 | <p>As a special case, if the object's base type is <tt>Class</tt> | 
|  | 948 | (possibly protocol-qualified), the type is adjusted to | 
|  | 949 | have <tt>__unsafe_unretained</tt> qualification instead.</p> | 
|  | 950 |  | 
|  | 951 | </div> | 
|  | 952 |  | 
|  | 953 | <div id="ownership.inference.indirect_parameters"> | 
|  | 954 | <h1>Indirect parameters</h1> | 
|  | 955 |  | 
|  | 956 | <p>If a function or method parameter has type <tt>T*</tt>, where | 
|  | 957 | <tt>T</tt> is an ownership-unqualified retainable object pointer type, | 
|  | 958 | then:</p> | 
|  | 959 |  | 
|  | 960 | <ul> | 
|  | 961 | <li>if <tt>T</tt> is <tt>const</tt>-qualified or <tt>Class</tt>, then | 
|  | 962 | it is implicitly qualified with <tt>__unsafe_unretained</tt>;</li> | 
|  | 963 | <li>otherwise, it is implicitly qualified | 
|  | 964 | with <tt>__autoreleasing</tt>.</li> | 
|  | 965 | </ul> | 
|  | 966 | </p> | 
|  | 967 |  | 
|  | 968 | <div class="rationale"><p>Rationale: <tt>__autoreleasing</tt> exists | 
|  | 969 | mostly for this case, the Cocoa convention for out-parameters.  Since | 
|  | 970 | a pointer to <tt>const</tt> is obviously not an out-parameter, we | 
|  | 971 | instead use a type more useful for passing arrays.  If the user | 
|  | 972 | instead intends to pass in a <em>mutable</em> array, inferring | 
|  | 973 | <tt>__autoreleasing</tt> is the wrong thing to do; this directs some | 
|  | 974 | of the caution in the following rules about writeback.</p></div> | 
|  | 975 |  | 
|  | 976 | <p>Such a type written anywhere else would be ill-formed by the | 
|  | 977 | general rule requiring ownership qualifiers.</p> | 
|  | 978 |  | 
|  | 979 | <p>This rule does not apply in Objective-C++ if a parameter's type is | 
|  | 980 | dependent in a template pattern and is only <em>instantiated</em> to | 
|  | 981 | a type which would be a pointer to an unqualified retainable object | 
|  | 982 | pointer type.  Such code is still ill-formed.</p> | 
|  | 983 |  | 
|  | 984 | <div class="rationale"><p>Rationale: the convention is very unlikely | 
|  | 985 | to be intentional in template code.</p></div> | 
|  | 986 |  | 
|  | 987 | </div> <!-- ownership.inference.indirect_parameters --> | 
| Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 988 |  | 
|  | 989 | <div id="ownership.inference.template_arguments"> | 
|  | 990 | <h1>Template arguments</h1> | 
|  | 991 |  | 
|  | 992 | <p>If a template argument for a template type parameter is an | 
|  | 993 | retainable object owner type that does not have an explicit ownership | 
|  | 994 | qualifier, it is adjusted to have <tt>__strong</tt> | 
| Douglas Gregor | 54fb28a | 2011-06-17 22:19:27 +0000 | [diff] [blame] | 995 | qualification. This adjustment occurs regardless of whether the | 
| Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 996 | template argument was deduced or explicitly specified. </p> | 
|  | 997 |  | 
|  | 998 | <div class="rationale"><p>Rationale: <tt>__strong</tt> is a useful default for containers (e.g., <tt>std::vector<id></tt>), which would otherwise require explicit qualification. Moreover, unqualified retainable object pointer types are unlikely to be useful within templates, since they generally need to have a qualifier applied to the before being used.</p></div> | 
|  | 999 |  | 
|  | 1000 | </div> <!-- ownership.inference.template_arguments --> | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 1001 | </div> <!-- ownership.inference --> | 
|  | 1002 | </div> <!-- ownership --> | 
|  | 1003 |  | 
| Douglas Gregor | e559ca1 | 2011-06-17 22:11:49 +0000 | [diff] [blame] | 1004 |  | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 1005 | <div id="family"> | 
|  | 1006 | <h1>Method families</h1> | 
|  | 1007 |  | 
|  | 1008 | <p>An Objective-C method may fall into a <span class="term">method | 
|  | 1009 | family</span>, which is a conventional set of behaviors ascribed to it | 
|  | 1010 | by the Cocoa conventions.</p> | 
|  | 1011 |  | 
|  | 1012 | <p>A method is in a certain method family if: | 
|  | 1013 | <ul> | 
|  | 1014 | <li>it has a <tt>objc_method_family</tt> attribute placing it in that | 
|  | 1015 | family; or if not that,</li> | 
|  | 1016 | <li>it does not have an <tt>objc_method_family</tt> attribute placing | 
|  | 1017 | it in a different or no family, and</li> | 
|  | 1018 | <li>its selector falls into the corresponding selector family, and</li> | 
|  | 1019 | <li>its signature obeys the added restrictions of the method family.</li> | 
|  | 1020 | </ul></p> | 
|  | 1021 |  | 
|  | 1022 | <p>A selector is in a certain selector family if, ignoring any leading | 
|  | 1023 | underscores, the first component of the selector either consists | 
|  | 1024 | entirely of the name of the method family or it begins with that name | 
|  | 1025 | followed by a character other than a lowercase letter.  For | 
|  | 1026 | example, <tt>_perform:with:</tt> and <tt>performWith:</tt> would fall | 
|  | 1027 | into the <tt>perform</tt> family (if we recognized one), | 
|  | 1028 | but <tt>performing:with</tt> would not.</p> | 
|  | 1029 |  | 
|  | 1030 | <p>The families and their added restrictions are:</p> | 
|  | 1031 |  | 
|  | 1032 | <ul> | 
|  | 1033 | <li><tt>alloc</tt> methods must return a retainable object pointer type.</li> | 
|  | 1034 | <li><tt>copy</tt> methods must return a retainable object pointer type.</li> | 
|  | 1035 | <li><tt>mutableCopy</tt> methods must return a retainable object pointer type.</li> | 
|  | 1036 | <li><tt>new</tt> methods must return a retainable object pointer type.</li> | 
|  | 1037 | <li><tt>init</tt> methods must be instance methods and must return an | 
|  | 1038 | Objective-C pointer type.  Additionally, a program is ill-formed if it | 
|  | 1039 | declares or contains a call to an <tt>init</tt> method whose return | 
|  | 1040 | type is neither <tt>id</tt> nor a pointer to a super-class or | 
|  | 1041 | sub-class of either the declaring class, if the method was declared on | 
|  | 1042 | a class, or the static receiver type of the call, if it was declared | 
|  | 1043 | on a protocol.</p> | 
|  | 1044 |  | 
|  | 1045 | <div class="rationale"><p>Rationale: there are a fair number of existing | 
|  | 1046 | methods with <tt>init</tt>-like selectors which nonetheless don't | 
|  | 1047 | follow the <tt>init</tt> conventions.  Typically these are either | 
|  | 1048 | accidental naming collisions or helper methods called during | 
|  | 1049 | initialization.  Because of the peculiar retain/release behavior | 
|  | 1050 | of <tt>init</tt> methods, it's very important not to treat these | 
|  | 1051 | methods as <tt>init</tt> methods if they aren't meant to be.  It was | 
|  | 1052 | felt that implicitly defining these methods out of the family based on | 
|  | 1053 | the exact relationship between the return type and the declaring class | 
|  | 1054 | would much too subtle and fragile.  Therefore we identify a small | 
|  | 1055 | number of legitimate-seeming return types and call everything else an | 
|  | 1056 | error.  This serves the secondary purpose of encouraging programmers | 
|  | 1057 | not to accidentally give methods names in the <tt>init</tt> family.</p></div> | 
|  | 1058 | </li> | 
|  | 1059 | </ul> | 
|  | 1060 |  | 
|  | 1061 | <p>A program is ill-formed if a method's declarations, | 
|  | 1062 | implementations, and overrides do not all have the same method | 
|  | 1063 | family.</p> | 
|  | 1064 |  | 
|  | 1065 | <div id="family.attribute"> | 
|  | 1066 | <h1>Explicit method family control</h1> | 
|  | 1067 |  | 
|  | 1068 | <p>A method may be annotated with the <tt>objc_method_family</tt> | 
|  | 1069 | attribute to precisely control which method family it belongs to.  If | 
|  | 1070 | a method in an <tt>@implementation</tt> does not have this attribute, | 
|  | 1071 | but there is a method declared in the corresponding <tt>@interface</tt> | 
|  | 1072 | that does, then the attribute is copied to the declaration in the | 
|  | 1073 | <tt>@implementation</tt>.  The attribute is available outside of ARC, | 
|  | 1074 | and may be tested for with the preprocessor query | 
|  | 1075 | <tt>__has_attribute(objc_method_family)</tt>.</p> | 
|  | 1076 |  | 
|  | 1077 | <p>The attribute is spelled | 
|  | 1078 | <tt>__attribute__((objc_method_family(<i>family</i>)))</tt>. | 
|  | 1079 | If <i>family</i> is <tt>none</tt>, the method has no family, even if | 
|  | 1080 | it would otherwise be considered to have one based on its selector and | 
|  | 1081 | type.  Otherwise, <i>family</i> must be one | 
|  | 1082 | of <tt>alloc</tt>, <tt>copy</tt>, <tt>init</tt>, | 
|  | 1083 | <tt>mutableCopy</tt>, or <tt>new</tt>, in which case the method is | 
|  | 1084 | considered to belong to the corresponding family regardless of its | 
|  | 1085 | selector.  It is an error if a method that is explicitly added to a | 
|  | 1086 | family in this way does not meet the requirements of the family other | 
|  | 1087 | than the selector naming convention.</p> | 
|  | 1088 |  | 
|  | 1089 | <div class="rationale"><p>Rationale: the rules codified in this document | 
|  | 1090 | describe the standard conventions of Objective-C.  However, as these | 
|  | 1091 | conventions have not heretofore been enforced by an unforgiving | 
|  | 1092 | mechanical system, they are only imperfectly kept, especially as they | 
|  | 1093 | haven't always even been precisely defined.  While it is possible to | 
|  | 1094 | define low-level ownership semantics with attributes like | 
|  | 1095 | <tt>ns_returns_retained</tt>, this attribute allows the user to | 
|  | 1096 | communicate semantic intent, which of use both to ARC (which, e.g., | 
|  | 1097 | treats calls to <tt>init</tt> specially) and the static analyzer.</p></div> | 
|  | 1098 | </div> | 
|  | 1099 |  | 
|  | 1100 | <div id="family.semantics"> | 
|  | 1101 | <h1>Semantics of method families</h1> | 
|  | 1102 |  | 
|  | 1103 | <p>A method's membership in a method family may imply non-standard | 
|  | 1104 | semantics for its parameters and return type.</p> | 
|  | 1105 |  | 
|  | 1106 | <p>Methods in the <tt>alloc</tt>, <tt>copy</tt>, <tt>mutableCopy</tt>, | 
|  | 1107 | and <tt>new</tt> families — that is, methods in all the | 
|  | 1108 | currently-defined families except <tt>init</tt> — transfer | 
|  | 1109 | ownership of a +1 retain count on their return value to the calling | 
|  | 1110 | function, as if they were implicitly annotated with | 
|  | 1111 | the <tt>ns_returns_retained</tt> attribute.  However, this is not true | 
|  | 1112 | if the method has either of the <tt>ns_returns_autoreleased</tt> or | 
|  | 1113 | <tt>ns_returns_not_retained</tt> attributes.</p> | 
|  | 1114 |  | 
|  | 1115 | <div id="family.semantics.init"> | 
|  | 1116 | <h1>Semantics of <tt>init</tt></h1> | 
|  | 1117 |  | 
|  | 1118 | <p>Methods in the <tt>init</tt> family must be transferred ownership | 
|  | 1119 | of a +1 retain count on their <tt>self</tt> parameter, exactly as if | 
|  | 1120 | the method had the <tt>ns_consumes_self</tt> attribute, and must | 
|  | 1121 | transfer ownership of a +1 retain count on their return value, exactly | 
|  | 1122 | as if they method had the <tt>ns_returns_retained</tt> attribute. | 
|  | 1123 | Neither of these may be altered through attributes.</p> | 
|  | 1124 |  | 
|  | 1125 | <p>A call to an <tt>init</tt> method with a receiver that is either | 
|  | 1126 | <tt>self</tt> (possibly parenthesized or casted) or <tt>super</tt> is | 
|  | 1127 | called a <span class="term">delegate init call</span>.  It is an error | 
|  | 1128 | for a delegate init call to be made except from an <tt>init</tt> | 
|  | 1129 | method, and excluding blocks within such methods.</p> | 
|  | 1130 |  | 
|  | 1131 | <p>The variable <tt>self</tt> is mutable in an <tt>init</tt> method | 
|  | 1132 | and is implicitly qualified as <tt>__strong</tt>.  However, a program | 
|  | 1133 | is ill-formed, no diagnostic required, if it alters <tt>self</tt> | 
|  | 1134 | except to assign it the immediate result of a delegate init call.  It | 
|  | 1135 | is an error to use the previous value of <tt>self</tt> after the | 
|  | 1136 | completion of a delegate init call.</p> | 
|  | 1137 |  | 
|  | 1138 | <p>A program is ill-formed, no diagnostic required, if it causes two | 
|  | 1139 | or more calls to <tt>init</tt> methods on the same object, except that | 
|  | 1140 | each <tt>init</tt> method invocation may perform at most one | 
|  | 1141 | delegate init call.</p> | 
|  | 1142 |  | 
| John McCall | f3d08a6 | 2011-06-18 07:31:30 +0000 | [diff] [blame^] | 1143 | </div> <!-- family.semantics.init --> | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 1144 |  | 
|  | 1145 | <div id="family.semantics.result_type"> | 
|  | 1146 | <h1>Related result types</h1> | 
|  | 1147 |  | 
|  | 1148 | <p>Certain methods are candidates to have <span class="term">related | 
|  | 1149 | result types</span>:</p> | 
|  | 1150 | <ul> | 
|  | 1151 | <li>class methods in the <tt>alloc</tt> and <tt>new</tt> method families</li> | 
|  | 1152 | <li>instance methods in the <tt>init</tt> family</li> | 
|  | 1153 | <li>the instance method <tt>self</tt></li> | 
|  | 1154 | <li>outside of ARC, the instance methods <tt>retain</tt> and <tt>autorelease</tt></li> | 
|  | 1155 | </ul> | 
|  | 1156 |  | 
|  | 1157 | <p>If the formal result type of such a method is <tt>id</tt> or | 
|  | 1158 | protocol-qualified <tt>id</tt>, or a type equal to the declaring class | 
|  | 1159 | or a superclass, then it is said to have a related result type.  In | 
|  | 1160 | this case, when invoked in an explicit message send, it is assumed to | 
|  | 1161 | return a type related to the type of the receiver:</p> | 
|  | 1162 |  | 
|  | 1163 | <ul> | 
|  | 1164 | <li>if it is a class method, and the receiver is a class | 
|  | 1165 | name <tt>T</tt>, the message send expression has type <tt>T*</tt>; | 
|  | 1166 | otherwise</li> | 
|  | 1167 | <li>if it is an instance method, and the receiver has type <tt>T</tt>, | 
|  | 1168 | the message send expression has type <tt>T</tt>; otherwise</li> | 
|  | 1169 | <li>the message send expression has the normal result type of the | 
|  | 1170 | method.</li> | 
|  | 1171 | </ul> | 
|  | 1172 |  | 
|  | 1173 | <p>This is a new rule of the Objective-C language and applies outside | 
|  | 1174 | of ARC.</p> | 
|  | 1175 |  | 
|  | 1176 | <div class="rationale"><p>Rationale: ARC's automatic code emission is | 
|  | 1177 | more prone than most code to signature errors, i.e. errors where a | 
|  | 1178 | call was emitted against one method signature, but the implementing | 
|  | 1179 | method has an incompatible signature.  Having more precise type | 
|  | 1180 | information helps drastically lower this risks, as well as catching | 
|  | 1181 | a number of latent bugs.</p></div> | 
|  | 1182 |  | 
|  | 1183 | </div> <!-- family.semantics.result_type --> | 
|  | 1184 | </div> <!-- family.semantics --> | 
|  | 1185 | </div> <!-- family --> | 
|  | 1186 |  | 
|  | 1187 | <div id="optimization"> | 
|  | 1188 | <h1>Optimization</h1> | 
|  | 1189 |  | 
|  | 1190 | <p>ARC applies aggressive rules for the optimization of local | 
|  | 1191 | behavior.  These rules are based around a core assumption of | 
|  | 1192 | <span class="term">local balancing</span>: that other code will | 
|  | 1193 | perform retains and releases as necessary (and only as necessary) for | 
|  | 1194 | its own safety, and so the optimizer does not need to consider global | 
|  | 1195 | properties of the retain and release sequence.  For example, if a | 
|  | 1196 | retain and release immediately bracket a call, the optimizer can | 
|  | 1197 | delete the retain and release on the assumption that the called | 
|  | 1198 | function will not do a constant number of unmotivated releases | 
|  | 1199 | followed by a constant number of <q>balancing</q> retains, such that | 
|  | 1200 | the local retain/release pair is the only thing preventing the called | 
|  | 1201 | function from ending up with a dangling reference.</p> | 
|  | 1202 |  | 
|  | 1203 | <p>The optimizer assumes that when a new value enters local control, | 
|  | 1204 | e.g. from a load of a non-local object or as the result of a function | 
|  | 1205 | call, it is instaneously valid.  Subsequently, a retain and release of | 
|  | 1206 | a value are necessary on a computation path only if there is a use of | 
|  | 1207 | that value before the release and after any operation which might | 
|  | 1208 | cause a release of the value (including indirectly or non-locally), | 
|  | 1209 | and only if the value is not demonstrably already retained.</p> | 
|  | 1210 |  | 
|  | 1211 | <p>The complete optimization rules are quite complicated, but it would | 
|  | 1212 | still be useful to document them here.</p> | 
|  | 1213 |  | 
|  | 1214 | </div> | 
|  | 1215 |  | 
|  | 1216 | <div id="misc"> | 
|  | 1217 | <h1>Miscellaneous</h1> | 
|  | 1218 |  | 
| John McCall | f3d08a6 | 2011-06-18 07:31:30 +0000 | [diff] [blame^] | 1219 | <div id="misc.special_methods"> | 
|  | 1220 | <h1>Special methods</h1> | 
|  | 1221 |  | 
|  | 1222 | <div id="misc.special_methods.retain"> | 
|  | 1223 | <h1>Memory management methods</h1> | 
|  | 1224 |  | 
|  | 1225 | <p>A program is ill-formed if it contains a method definition, message | 
|  | 1226 | send, or <tt>@selector</tt> expression for any of the following | 
|  | 1227 | selectors: | 
|  | 1228 | <ul> | 
|  | 1229 | <li><tt>autorelease</tt></li> | 
|  | 1230 | <li><tt>release</tt></li> | 
|  | 1231 | <li><tt>retain</tt></li> | 
|  | 1232 | <li><tt>retainCount</tt></li> | 
|  | 1233 | </ul> | 
|  | 1234 | </p> | 
|  | 1235 |  | 
|  | 1236 | <div class="rationale"><p>Rationale: <tt>retainCount</tt> is banned | 
|  | 1237 | because ARC robs it of consistent semantics.  The others were banned | 
|  | 1238 | after weighing three options for how to deal with message sends:</p> | 
|  | 1239 |  | 
|  | 1240 | <p><b>Honoring</b> them would work out very poorly if a programmer | 
|  | 1241 | naively or accidentally tried to incorporate code written for manual | 
|  | 1242 | retain/release code into an ARC program.  At best, such code would do | 
|  | 1243 | twice as much work as necessary; quite frequently, however, ARC and | 
|  | 1244 | the explicit code would both try to balance the same retain, leading | 
|  | 1245 | to crashes.  The cost is losing the ability to perform <q>unrooted</q> | 
|  | 1246 | retains, i.e. retains not logically corresponding to a strong | 
|  | 1247 | reference in the object graph.</p> | 
|  | 1248 |  | 
|  | 1249 | <p><b>Ignoring</b> them would badly violate user expectations about their | 
|  | 1250 | code.  While it <em>would</em> make it easier to develop code simultaneously | 
|  | 1251 | for ARC and non-ARC, there is very little reason to do so except for | 
|  | 1252 | certain library developers.  ARC and non-ARC translation units share | 
|  | 1253 | an execution model and can seamlessly interoperate.  Within a | 
|  | 1254 | translation unit, a developer who faithfully maintains their code in | 
|  | 1255 | non-ARC mode is suffering all the restrictions of ARC for zero | 
|  | 1256 | benefit, while a developer who isn't testing the non-ARC mode is | 
|  | 1257 | likely to be unpleasantly surprised if they try to go back to it.</p> | 
|  | 1258 |  | 
|  | 1259 | <p><b>Banning</b> them has the disadvantage of making it very awkward | 
|  | 1260 | to migrate existing code to ARC.  The best answer to that, given a | 
|  | 1261 | number of other changes and restrictions in ARC, is to provide a | 
|  | 1262 | specialized tool to assist users in that migration.</p> | 
|  | 1263 |  | 
|  | 1264 | <p>Implementing these methods was banned because they are too integral | 
|  | 1265 | to the semantics of ARC; many tricks which worked tolerably under | 
|  | 1266 | manual reference counting will misbehave if ARC performs an ephemeral | 
|  | 1267 | extra retain or two.  If absolutely required, it is still possible to | 
|  | 1268 | implement them in non-ARC code, for example in a category; the | 
|  | 1269 | implementations must obey the <a href="#objects.retains">semantics</a> | 
|  | 1270 | laid out elsewhere in this document.</p> | 
|  | 1271 |  | 
|  | 1272 | </div> | 
|  | 1273 | </div> <!-- misc.special_methods.retain --> | 
|  | 1274 |  | 
|  | 1275 | <div id="misc.special_methods.dealloc"> | 
|  | 1276 | <h1><tt>dealloc</tt></h1> | 
|  | 1277 |  | 
|  | 1278 | <p>A program is ill-formed if it contains a message send | 
|  | 1279 | or <tt>@selector</tt> expression for the selector <tt>dealloc</tt>.</p> | 
|  | 1280 |  | 
|  | 1281 | <div class="rationale"><p>Rationale: there are no legitimate reasons | 
|  | 1282 | to call <tt>dealloc</tt> directly.</p></div> | 
|  | 1283 |  | 
|  | 1284 | <p>A class may provide a method definition for an instance method | 
|  | 1285 | named <tt>dealloc</tt>.  This method will be called after the final | 
|  | 1286 | <tt>release</tt> of the object but before it is deallocated or any of | 
|  | 1287 | its instance variables are destroyed.  The superclass's implementation | 
|  | 1288 | of <tt>dealloc</tt> will be called automatically when the method | 
|  | 1289 | returns.</p> | 
|  | 1290 |  | 
|  | 1291 | <div class="rationale"><p>Rationale: even though ARC destroys instance | 
|  | 1292 | variables automatically, there are still legitimate reasons to write | 
|  | 1293 | a <tt>dealloc</tt> method, such as freeing non-retainable resources. | 
|  | 1294 | Failing to call <tt>[super dealloc]</tt> in such a method is nearly | 
|  | 1295 | always a bug.  Sometimes, the object is simply trying to prevent | 
|  | 1296 | itself from being destroyed, but <tt>dealloc</tt> is really far too | 
|  | 1297 | late for the object to be raising such objections.  Somewhat more | 
|  | 1298 | legitimately, an object may have been pool-allocated and should not be | 
|  | 1299 | deallocated with <tt>free</tt>; for now, this can only be supported | 
|  | 1300 | with a <tt>dealloc</tt> implementation outside of ARC.  Such an | 
|  | 1301 | implementation must be very careful to do all the other work | 
|  | 1302 | that <tt>NSObject</tt>'s <tt>dealloc</tt> would, which is outside the | 
|  | 1303 | scope of this document to describe.</p></div> | 
|  | 1304 |  | 
|  | 1305 | </div> | 
|  | 1306 |  | 
|  | 1307 | </div> <!-- misc.special_methods --> | 
|  | 1308 |  | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 1309 | <div id="autoreleasepool"> | 
|  | 1310 | <h1><tt>@autoreleasepool</tt></h1> | 
|  | 1311 |  | 
|  | 1312 | <p>To simplify the use of autorelease pools, and to bring them under | 
|  | 1313 | the control of the compiler, a new kind of statement is available in | 
|  | 1314 | Objective-C.  It is written <tt>@autoreleasepool</tt> followed by | 
|  | 1315 | a <i>compound-statement</i>, i.e. by a new scope delimited by curly | 
|  | 1316 | braces.  Upon entry to this block, the current state of the | 
|  | 1317 | autorelease pool is captured.  When the block is exited normally, | 
|  | 1318 | whether by fallthrough or directed control flow (such | 
|  | 1319 | as <tt>return</tt> or <tt>break</tt>), the autorelease pool is | 
|  | 1320 | restored to the saved state, releasing all the objects in it.  When | 
|  | 1321 | the block is exited with an exception, the pool is not drained.</p> | 
|  | 1322 |  | 
| John McCall | f3d08a6 | 2011-06-18 07:31:30 +0000 | [diff] [blame^] | 1323 | <p><tt>@autoreleasepool</tt> may be used in non-ARC translation units, | 
|  | 1324 | with equivalent semantics.</p> | 
|  | 1325 |  | 
| John McCall | 8246702 | 2011-06-15 21:21:53 +0000 | [diff] [blame] | 1326 | <p>A program is ill-formed if it refers to the | 
|  | 1327 | <tt>NSAutoreleasePool</tt> class.</p> | 
|  | 1328 |  | 
|  | 1329 | <div class="rationale"><p>Rationale: autorelease pools are clearly | 
|  | 1330 | important for the compiler to reason about, but it is far too much to | 
|  | 1331 | expect the compiler to accurately reason about control dependencies | 
|  | 1332 | between two calls.  It is also very easy to accidentally forget to | 
|  | 1333 | drain an autorelease pool when using the manual API, and this can | 
|  | 1334 | significantly inflate the process's high-water-mark.  The introduction | 
|  | 1335 | of a new scope is unfortunate but basically required for sane | 
|  | 1336 | interaction with the rest of the language.  Not draining the pool | 
|  | 1337 | during an unwind is apparently required by the Objective-C exceptions | 
|  | 1338 | implementation.</p></div> | 
|  | 1339 |  | 
|  | 1340 | </div> <!-- autoreleasepool --> | 
|  | 1341 |  | 
|  | 1342 | <div id="misc.self"> | 
|  | 1343 | <h1><tt>self</tt></h1> | 
|  | 1344 |  | 
|  | 1345 | <p>The <tt>self</tt> parameter variable of an Objective-C method is | 
|  | 1346 | never actually retained by the implementation.  It is undefined | 
|  | 1347 | behavior, or at least dangerous, to cause an object to be deallocated | 
|  | 1348 | during a message send to that object.  To make this | 
|  | 1349 | safe, <tt>self</tt> is implicitly <tt>const</tt> unless the method is | 
|  | 1350 | in the <a href="#family.semantics.init"><tt>init</tt> family</a>.</p> | 
|  | 1351 |  | 
|  | 1352 | <div class="rationale"><p>Rationale: the cost of | 
|  | 1353 | retaining <tt>self</tt> in all methods was found to be prohibitive, as | 
|  | 1354 | it tends to be live across calls, preventing the optimizer from | 
|  | 1355 | proving that the retain and release are unnecessary — for good | 
|  | 1356 | reason, as it's quite possible in theory to cause an object to be | 
|  | 1357 | deallocated during its execution without this retain and release. | 
|  | 1358 | Since it's extremely uncommon to actually do so, even unintentionally, | 
|  | 1359 | and since there's no natural way for the programmer to remove this | 
|  | 1360 | retain/release pair otherwise (as there is for other parameters by, | 
|  | 1361 | say, making the variable <tt>__unsafe_unretained</tt>), we chose to | 
|  | 1362 | make this optimizing assumption and shift some amount of risk to the | 
|  | 1363 | user.</p></div> | 
|  | 1364 |  | 
|  | 1365 | </div> <!-- misc.self --> | 
|  | 1366 |  | 
|  | 1367 | <div id="misc.enumeration"> | 
|  | 1368 | <h1>Fast enumeration iteration variables</h1> | 
|  | 1369 |  | 
|  | 1370 | <p>If a variable is declared in the condition of an Objective-C fast | 
|  | 1371 | enumeration loop, and the variable has no explicit ownership | 
|  | 1372 | qualifier, then it is qualified with <tt>const __strong</tt> and | 
|  | 1373 | objects encountered during the enumeration are not actually | 
|  | 1374 | retained.</p> | 
|  | 1375 |  | 
|  | 1376 | <div class="rationale"><p>Rationale: this is an optimization made | 
|  | 1377 | possible because fast enumeration loops promise to keep the objects | 
|  | 1378 | retained during enumeration, and the collection itself cannot be | 
|  | 1379 | synchronously modified.  It can be overridden by explicitly qualifying | 
|  | 1380 | the variable with <tt>__strong</tt>, which will make the variable | 
|  | 1381 | mutable again and cause the loop to retain the objects it | 
|  | 1382 | encounters.</div> | 
|  | 1383 |  | 
|  | 1384 | </div> | 
|  | 1385 |  | 
|  | 1386 | <div id="misc.blocks"> | 
|  | 1387 | <h1>Blocks</h1> | 
|  | 1388 |  | 
|  | 1389 | <p>The implicit <tt>const</tt> capture variables created when | 
|  | 1390 | evaluating a block literal expression have the same ownership | 
|  | 1391 | semantics as the local variables they capture.  The capture is | 
|  | 1392 | performed by reading from the captured variable and initializing the | 
|  | 1393 | capture variable with that value; the capture variable is destroyed | 
|  | 1394 | when the block literal is, i.e. at the end of the enclosing scope.</p> | 
|  | 1395 |  | 
|  | 1396 | <p>The <a href="#ownership.inference">inference</a> rules apply | 
|  | 1397 | equally to <tt>__block</tt> variables, which is a shift in semantics | 
|  | 1398 | from non-ARC, where <tt>__block</tt> variables did not implicitly | 
|  | 1399 | retain during capture.</p> | 
|  | 1400 |  | 
|  | 1401 | <p><tt>__block</tt> variables of retainable object owner type are | 
|  | 1402 | moved off the stack by initializing the heap copy with the result of | 
|  | 1403 | moving from the stack copy.</tt></p> | 
|  | 1404 |  | 
|  | 1405 | <p>With the exception of retains done as part of initializing | 
|  | 1406 | a <tt>__strong</tt> parameter variable or reading a <tt>__weak</tt> | 
|  | 1407 | variable, whenever these semantics call for retaining a value of | 
|  | 1408 | block-pointer type, it has the effect of a <tt>Block_copy</tt>.  The | 
|  | 1409 | optimizer may remove such copies when it sees that the result is | 
|  | 1410 | used only as an argument to a call.</p> | 
|  | 1411 |  | 
|  | 1412 | </div> <!-- misc.blocks --> | 
|  | 1413 |  | 
|  | 1414 | <div id="misc.exceptions"> | 
|  | 1415 | <h1>Exceptions</h1> | 
|  | 1416 |  | 
|  | 1417 | <p>By default in Objective C, ARC is not exception-safe for normal | 
|  | 1418 | releases: | 
|  | 1419 | <ul> | 
|  | 1420 | <li>It does not end the lifetime of <tt>__strong</tt> variables when | 
|  | 1421 | their scopes are abnormally terminated by an exception.</li> | 
|  | 1422 | <li>It does not perform releases which would occur at the end of | 
|  | 1423 | a full-expression if that full-expression throws an exception.</li> | 
|  | 1424 | </ul> | 
|  | 1425 |  | 
|  | 1426 | <p>A program may be compiled with the option | 
|  | 1427 | <tt>-fobjc-arc-exceptions</tt> in order to enable these, or with the | 
|  | 1428 | option <tt>-fno-objc-arc-exceptions</tt> to explicitly disable them, | 
|  | 1429 | with the last such argument <q>winning</q>.</p> | 
|  | 1430 |  | 
|  | 1431 | <div class="rationale"><p>Rationale: the standard Cocoa convention is | 
|  | 1432 | that exceptions signal programmer error and are not intended to be | 
|  | 1433 | recovered from.  Making code exceptions-safe by default would impose | 
|  | 1434 | severe runtime and code size penalties on code that typically does not | 
|  | 1435 | actually care about exceptions safety.  Therefore, ARC-generated code | 
|  | 1436 | leaks by default on exceptions, which is just fine if the process is | 
|  | 1437 | going to be immediately terminated anyway.  Programs which do care | 
|  | 1438 | about recovering from exceptions should enable the option.</p></div> | 
|  | 1439 |  | 
|  | 1440 | <p>In Objective-C++, <tt>-fobjc-arc-exceptions</tt> is enabled by | 
|  | 1441 | default.</p> | 
|  | 1442 |  | 
|  | 1443 | <div class="rationale"><p>Rationale: C++ already introduces pervasive | 
|  | 1444 | exceptions-cleanup code of the sort that ARC introduces.  C++ | 
|  | 1445 | programmers who have not already disabled exceptions are much more | 
|  | 1446 | likely to actual require exception-safety.</p></div> | 
|  | 1447 |  | 
|  | 1448 | <p>ARC does end the lifetimes of <tt>__weak</tt> objects when an | 
|  | 1449 | exception terminates their scope unless exceptions are disabled in the | 
|  | 1450 | compiler.</p> | 
|  | 1451 |  | 
|  | 1452 | <div class="rationale"><p>Rationale: the consequence of a | 
|  | 1453 | local <tt>__weak</tt> object not being destroyed is very likely to be | 
|  | 1454 | corruption of the Objective-C runtime, so we want to be safer here. | 
|  | 1455 | Of course, potentially massive leaks are about as likely to take down | 
|  | 1456 | the process as this corruption is if the program does try to recover | 
|  | 1457 | from exceptions.</p></div> | 
|  | 1458 |  | 
|  | 1459 | </div> <!-- misc.exceptions --> | 
|  | 1460 |  | 
|  | 1461 | </div> <!-- misc --> | 
|  | 1462 | </div> <!-- root --> | 
|  | 1463 | </body> | 
|  | 1464 | </html> |