blob: 1150614254f85f5a03967827057ddab7169e6113 [file] [log] [blame]
John McCall82467022011-06-15 21:21:53 +00001<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. */
8div#toc ul {
9 padding-left: 0
10}
11
12/* Rationales appear in italic. */
13div.rationale {
14 font-style: italic
15}
16
John McCallf3d08a62011-06-18 07:31:30 +000017div.rationale em {
18 font-style: normal
19}
20
John McCall82467022011-06-15 21:21:53 +000021div h1 { font-size: 2em; margin: .67em 0 }
22div div h1 { font-size: 1.5em; margin: .75em 0 }
23div div div h1 { font-size: 1.17em; margin: .83em 0 }
24div div div div h1 { margin: 1.12em 0 }
25
26span.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.
31function 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
69function onLoad() {
70 var toc = document.getElementById("toc");
71 var content = document.getElementById("content");
72 buildTOC(content, toc, "");
73}
74window.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
95complete technical specification of Automatic Reference Counting.
96Given a core Objective-C compiler and runtime, it should be possible
97to write a compiler and runtime which implements these new
98semantics.</p>
99
100<p>The secondary purpose is to act as a rationale for why ARC was
101designed in this way. This should remain tightly focused on the
102technical 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
112creating anonymous functions. Users interact with and transfer block
113objects using <span class="term">block pointers</span>, which are
114represented like a normal pointer. A block may capture values from
115local variables; when this occurs, memory must be dynamically
116allocated. The initial allocation is done on the stack, but the
117runtime provides a <tt>Block_copy</tt> function which, given a block
118pointer, either copies the underlying block object to the heap,
119setting its reference count to 1 and returning the new block pointer,
120or (if the block object is already on the heap) increases its
121reference count by 1. The paired function is <tt>Block_release</tt>,
122which decreases the reference count by 1 and destroys the object if
123the count reaches zero and is on the heap.</p>
124
125<p>Objective-C is a set of language extensions, significant enough to
126be considered a different language. It is a strict superset of C.
127The extensions can also be imposed on C++, producing a language called
128Objective-C++. The primary feature is a single-inheritance object
129system; we briefly describe the modern dialect.</p>
130
131<p>Objective-C defines a new type kind, collectively called
132the <span class="term">object pointer types</span>. This kind has two
133notable builtin members, <tt>id</tt> and <tt>Class</tt>; <tt>id</tt>
134is the final supertype of all object pointers. The validity of
135conversions between object pointer types is not checked at runtime.
136Users may define <span class="term">classes</span>; each class is a
137type, and the pointer to that type is an object pointer type. A class
138may have a superclass; its pointer type is a subtype of its
139superclass's pointer type. A class has a set
140of <span class="term">ivars</span>, fields which appear on all
141instances of that class. For every class <i>T</i> there's an
142associated metaclass; it has no fields, its superclass is the
143metaclass of <i>T</i>'s superclass, and its metaclass is a global
144class. Every class has a global object whose class is the
145class's metaclass; metaclasses have no associated type, so pointers to
146this object have type <tt>Class</tt>.</p>
147
148<p>A class declaration (<tt>@interface</tt>) declares a set
149of <span class="term">methods</span>. A method has a return type, a
150list of argument types, and a <span class="term">selector</span>: a
151name like <tt>foo:bar:baz:</tt>, where the number of colons
152corresponds to the number of formal arguments. A method may be an
153instance method, in which case it can be invoked on objects of the
154class, or a class method, in which case it can be invoked on objects
155of the metaclass. A method may be invoked by providing an object
156(called the <span class="term">receiver</span>) and a list of formal
157arguments 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
162this name, then in that class's superclass, etc., until it finds
163something it can execute. The receiver <q>expression</q> may also be
164the name of a class, in which case the actual receiver is the class
165object for that class, or (within method definitions) it may
166be <tt>super</tt>, in which case the lookup algorithm starts with the
167static superclass instead of the dynamic class. The actual methods
168dynamically 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
171call, 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
176associated with any class, but which classes may claim to follow.
177Object pointer types may be qualified with additional protocols that
178the object is known to support.</p>
179
180<p><span class="term">Class extensions</span> are collections of ivars
181and methods, designed to allow a class's <tt>@interface</tt> to be
182split across multiple files; however, there is still a primary
183implementation file which must see the <tt>@interface</tt>s of all
184class extensions.
185<span class="term">Categories</span> allow methods (but not ivars) to
186be declared <i>post hoc</i> on an arbitrary class; the methods in the
187category's <tt>@implementation</tt> will be dynamically added to that
188class's method tables which the category is loaded at runtime,
189replacing those methods in case of a collision.</p>
190
191<p>In the standard environment, objects are allocated on the heap, and
192their lifetime is manually managed using a reference count. This is
193done using two instance methods which all classes are expected to
194implement: <tt>retain</tt> increases the object's reference count by
1951, whereas <tt>release</tt> decreases it by 1 and calls the instance
196method <tt>dealloc</tt> if the count reaches 0. To simplify certain
197operations, there is also an <span class="term">autorelease
198pool</span>, a thread-local list of objects to call <tt>release</tt>
199on later; an object can be added to this pool by
200calling <tt>autorelease</tt> on it.</p>
201
202<p>Block pointers may be converted to type <tt>id</tt>; block objects
203are laid out in a way that makes them compatible with Objective-C
204objects. There is a builtin class that all block objects are
205considered to be objects of; this class implements <tt>retain</tt> by
206adjusting 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
216for Objective-C objects and blocks, freeing the programmer from the
217need explicitly insert retains and releases. It does not provide a
218cycle collector; users must explicitly manage lifetime instead.</p>
219
220<p>ARC may be explicitly enabled with the compiler
221flag <tt>-fobjc-arc</tt>. It may also be explicitly disabled with the
222compiler flag <tt>-fno-objc-arc</tt>. The last of these two flags
223appearing on the compile line <q>wins</q>.</p>
224
225<p>If ARC is enabled, <tt>__has_feature(objc_arc)</tt> will expand to
2261 in the preprocessor. For more information about <tt>__has_feature</tt>,
227see the <a href="LanguageExtensions.html#__has_feature_extension">language
228extensions</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
236operations, and the restrictions imposed on their use under ARC. Note
237in particular that it covers the rules for pointer <em>values</em>
238(patterns of bits indicating the location of a pointed-to object), not
239pointer
240<em>objects</em> (locations in memory which store pointer values).
241The 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
245a <span class="term">retainable object pointer type</span>
246(<q>retainable type</q>). There are three kinds of retainable object
247pointer types:</p>
248<ul>
249<li>block pointers (formed by applying the caret (<tt>^</tt>)
250declarator 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>,
256are 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
261all code to be recompiled with ARC; therefore, ARC must interoperate
262with Objective-C code which manages retains and releases manually. In
263general, there are three requirements in order for a
264compiler-supported reference-count system to provide reliable
265interoperation:</p>
266
267<ul>
268<li>The type system must reliably identify which objects are to be
269managed. An <tt>int*</tt> might be a pointer to a <tt>malloc</tt>'ed
270array, or it might be a interior pointer to such an array, or it might
271point to some field or local variable. In contrast, values of the
272retainable object pointer types are never interior.</li>
273<li>The type system must reliably indicate how to
274manage objects of a type. This usually means that the type must imply
275a procedure for incrementing and decrementing retain counts.
276Supporting single-ownership objects requires a lot more explicit
277mediation in the language.</li>
278<li>There must be reliable conventions for whether and
279when <q>ownership</q> is passed between caller and callee, for both
280arguments and return values. Objective-C methods follow such a
281convention very reliably, at least for system libraries on Mac OS X,
282and functions always pass objects at +0. The C-based APIs for Core
283Foundation objects, on the other hand, have much more varied transfer
284semantics.</li>
285</ul>
286</div> <!-- rationale -->
287
288<p>The use of <tt>__attribute__((NSObject))</tt> typedefs is not
289recommended. If it's absolutely necessary to use this attribute, be
290very explicit about using the typedef, and do not assume that it will
291be preserved by language features like <tt>__typeof</tt> and C++
292template argument substitution.</p>
293
294<div class="rationale"><p>Rationale: any compiler operation which
295incidentally strips type <q>sugar</q> from a type will yield a type
296without the attribute, which may result in unexpected
297behavior.</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
303pointer</span> or a pointer to a valid object. Furthermore, if it has
304block pointer type and is not <tt>null</tt> then it must actually be a
305pointer to a block object, and if it has <tt>Class</tt> type (possibly
306protocol-qualified) then it must actually be a pointer to a class
307object. Otherwise ARC does not enforce the Objective-C type system as
308long as the implementing methods follow the signature of the static
309type. It is undefined behavior if ARC is exposed to an invalid
310pointer.</p>
311
312<p>For ARC's purposes, a valid object is one with <q>well-behaved</q>
313retaining operations. Specifically, the object must be laid out such
314that the Objective-C message send machinery can successfully send it
315the following messages:</p>
316
317<ul>
318<li><tt>retain</tt>, taking no arguments and returning a pointer to
319the 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
322to the object.</li>
323</ul>
324
325<p>The behavior of these methods is constrained in the following ways.
326The term <span class="term">high-level semantics</span> is an
327intentionally vague term; the intent is that programmers must
328implement these methods in a way such that the compiler, modifying
329code in ways it deems safe according to these constraints, will not
330violate their requirements. For example, if the user puts logging
331statements in <tt>retain</tt>, they should not be surprised if those
332statements are executed more or less often depending on optimization
333settings. These constraints are not exhaustive of the optimization
334opportunities: values held in local variables are subject to
335additional restrictions, described later in this document.</p>
336
337<p>It is undefined behavior if a computation history featuring a send
338of <tt>retain</tt> followed by a send of <tt>release</tt> to the same
339object, with no intervening <tt>release</tt> on that object, is not
340equivalent under the high-level semantics to a computation
341history in which these sends are removed. Note that this implies that
342these methods may not raise exceptions.</p>
343
344<p>It is undefined behavior if a computation history features any use
345whatsoever of an object following the completion of a send
346of <tt>release</tt> that is not preceded by a send of <tt>retain</tt>
347to 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
351is popped. It may not throw an exception.</p>
352
353<p>When the semantics call for performing one of these operations on a
354retainable object pointer, if that pointer is <tt>null</tt> then the
355effect is a no-op.</p>
356
357<p>All of the semantics described in this document are subject to
358additional <a href="#optimization">optimization rules</a> which permit
359the removal or optimization of operations based on local knowledge of
360data flow. The semantics describe the high-level behaviors that the
361compiler implements, not an exact sequence of operations that a
362program 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
370simply using a retainable object pointer as an operand within an
371expression. 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
376method, and</li>
377<li>receiving a retainable pointer as the result of a function or
378method call.</li>
379</ul>
380
381<div class="rationale"><p>Rationale: while this might seem
382uncontroversial, it is actually unsafe when multiple expressions are
383evaluated in <q>parallel</q>, as with binary operators and calls,
384because (for example) one expression might load from an object while
385another writes to it. However, C and C++ already call this undefined
386behavior because the evaluations are unsequenced, and ARC simply
387exploits that here to avoid needing to retain arguments across a large
388number of calls.</p></div>
389
390<p>The remainder of this section describes exceptions to these rules,
391how those exceptions are detected, and what those exceptions imply
392semantically.</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
398may be marked as <span class="term">consumed</span>, signifying that
399the callee expects to take ownership of a +1 retain count. This is
400done by adding the <tt>ns_consumed</tt> attribute to the parameter
401declaration, 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
407the type of the parameter. It controls only how the argument is
408passed and received.</p>
409
410<p>When passing such an argument, ARC retains the argument prior to
411making the call.</p>
412
413<p>When receiving such an argument, ARC releases the argument at the
414end of the function, subject to the usual optimizations for local
415values.</p>
416
417<div class="rationale"><p>Rationale: this formalizes direct transfers
418of ownership from a caller to a callee. The most common scenario here
419is passing the <tt>self</tt> parameter to <tt>init</tt>, but it is
420useful to generalize. Typically, local optimization will remove any
421extra retains and releases: on the caller side the retain will be
422merged with a +1 source, and on the callee side the release will be
423rolled into the initialization of the parameter.</p></div>
424
425<p>The implicit <tt>self</tt> parameter of a method may be marked as
426consumed by adding <tt>__attribute__((ns_consumes_self))</tt> to the
427method declaration. Methods in
428the <tt>init</tt> <a href="#family">family</a> are implicitly
429marked <tt>__attribute__((ns_consumes_self))</tt>.</p>
430
431<p>It is undefined behavior if an Objective-C message send of a method
432with <tt>ns_consumed</tt> parameters (other than self) is made to a
433null pointer.</p>
434
435<div class="rationale"><p>Rationale: in fact, it's probably a
436guaranteed 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
444may be marked as returning a retained value, signifying that the
445caller expects to take ownership of a +1 retain count. This is done
446by adding the <tt>ns_returns_retained</tt> attribute to the function or
447method 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
455value at the point of evaluation of the return statement, before
456leaving all local scopes.</p>
457
458<p>When receiving a return result from such a function or method, ARC
459releases the value at the end of the full-expression it is contained
460within, subject to the usual optimizations for local values.</p>
461
462<div class="rationale"><p>Rationale: this formalizes direct transfers of
463ownership from a callee to a caller. The most common scenario this
464models 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
466the frameworks. After optimization there are typically no extra
467retains and releases required.</p></div>
468
469<p>Methods in
470the <tt>alloc</tt>, <tt>copy</tt>, <tt>init</tt>, <tt>mutableCopy</tt>,
471and <tt>new</tt> <a href="#family">families</a> are implicitly marked
472<tt>__attribute__((ns_returns_retained))</tt>. This may be suppressed
473by explicitly marking the
474method <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
481does not return a retained value must ensure that the object is
482still valid across the return boundary.</p>
483
484<p>When returning from such a function or method, ARC retains the
485value at the point of evaluation of the return statement, then leaves
486all local scopes, and then balances out the retain while ensuring that
487the value lives across the call boundary. In the worst case, this may
488involve an <tt>autorelease</tt>, but callers must not assume that the
489value is actually in the autorelease pool.</p>
490
491<p>ARC performs no extra mandatory work on the caller side, although
492it may elect to do something to shorten the lifetime of the returned
493value.</p>
494
495<div class="rationale"><p>Rationale: it is common in non-ARC code to not
496return an autoreleased value; therefore the convention does not force
497either path. It is convenient to not be required to do unnecessary
498retains and autoreleases; this permits optimizations such as eliding
499retain/autoreleases when it can be shown that the original pointer
500will still be valid at the point of return.</p></div>
501
502<p>A method or function may be marked
503with <tt>__attribute__((ns_returns_autoreleased))</tt> to indicate
504that it returns a pointer which is guaranteed to be valid at least as
505long as the innermost autorelease pool. There are no additional
506semantics enforced in the definition of such a method; it merely
507enables 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
514annotated with one of three keywords:</p>
515
516<ul>
517<li><tt>(__bridge T) op</tt> casts the operand to the destination
518type <tt>T</tt>. If <tt>T</tt> is a retainable object pointer type,
519then <tt>op</tt> must have a non-retainable pointer type.
520If <tt>T</tt> is a non-retainable pointer type, then <tt>op</tt> must
521have a retainable object pointer type. Otherwise the cast is
522ill-formed. There is no transfer of ownership, and ARC inserts
523no retain operations.</li>
524
525<li><tt>(__bridge_retained T) op</tt> casts the operand, which must
526have retainable object pointer type, to the destination type, which
527must be a non-retainable pointer type. ARC retains the value, subject
528to the usual optimizations on local values, and the recipient is
529responsible for balancing that +1.</li>
530
531<li><tt>(__bridge_transfer T) op</tt> casts the operand, which must
532have non-retainable pointer type, to the destination type, which must
533be a retainable object pointer type. ARC will release the value at
534the end of the enclosing full-expression, subject to the usual
535optimizations on local values.</li>
536</ul>
537
538<p>These casts are required in order to transfer objects in and out of
539ARC control; see the rationale in the section
540on <a href="#objects.restrictions.conversion">conversion of retainable
541object pointers</a>.</p>
542
543<p>Using a <tt>__bridge_retained</tt> or <tt>__bridge_transfer</tt>
544cast purely to convince ARC to emit an unbalanced retain or release,
545respectively, 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
558convert a value of retainable object pointer type to any
559non-retainable type, or vice-versa, is ill-formed. For example, an
560Objective-C object pointer shall not be converted to <tt>intptr_t</tt>
561or <tt>void*</tt>. The <a href="#objects.operands.casts">bridged
562casts</a> may be used to perform these conversions where
563necessary.</p>
564
565<div class="rationale"><p>Rationale: we cannot ensure the correct
566management of the lifetime of objects if they may be freely passed
567around as unmanaged types. The bridged casts are provided so that the
568programmer may explicitly describe whether the cast transfers control
569into or out of ARC.</p></div>
570</div>
571
572<p>An unbridged cast to a retainable object pointer type of the return
573value of a Objective-C message send which yields a non-retainable
574pointer is treated as a <tt>__bridge_transfer</tt> cast
575if:</p>
576
577<ul>
578<li>the method has the <tt>cf_returns_retained</tt> attribute, or if
579not that,</li>
580<li>the method does not have the <tt>cf_returns_not_retained</tt>
581attribute and</li>
582<li>the method's <a href="#family">selector family</a> would imply
583the <tt>ns_returns_retained</tt> attribute on a method which returned
584a 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
597retainable object pointer type; that is, locations in memory which
598store retainable object pointers.</p>
599
600<p>A type is a <span class="term">retainable object owner type</span>
601if it is a retainable object pointer type or an array type whose
602element type is a retainable object owner type.</p>
603
604<p>An <span class="term">ownership qualifier</span> is a type
Douglas Gregor4020cae2011-06-17 23:16:24 +0000605qualifier which applies only to retainable object owner types. An array type is
606ownership-qualified according to its element type, and adding an ownership
607qualifier 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 McCall82467022011-06-15 21:21:53 +0000610to a type which is already ownership-qualified, even if it is the same
Douglas Gregor4020cae2011-06-17 23:16:24 +0000611qualifier. There is a single exception to this rule: an ownership qualifier
612may be applied to a substituted template type parameter, which overrides the
613ownership qualifier provided by the template argument.</p>
John McCall82467022011-06-15 21:21:53 +0000614
615<p>Except as described under
616the <a href="#ownership.inference">inference rules</a>, a program is
617ill-formed if it attempts to form a pointer or reference type to a
618retainable object owner type which lacks an ownership qualifier.</p>
619
620<div class="rationale"><p>Rationale: these rules, together with the
621inference rules, ensure that all objects and lvalues of retainable
Douglas Gregor4020cae2011-06-17 23:16:24 +0000622object 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 McCall82467022011-06-15 21:21:53 +0000623
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>
634if 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
641implementation. A program may not assume that they are or are not
642implemented with macros, or what those macros expand to.</p>
643
644<p>An ownership qualifier may be written anywhere that any other type
645qualifier may be written.</p>
646
647<p>If an ownership qualifier appears in
648the <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
652qualifier applies to that type;</li>
653<li>if the outermost non-array part of the declarator is a pointer or
654block 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
659declared object, it is applied to outermost pointer or block-pointer
660type.</p>
661
662<p>If an ownership qualifier appears anywhere else in a declarator, it
663applies 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
671may be performed on an object of retainable object pointer type. Each
672qualifier specifies different semantics for each of these operations.
673It is still undefined behavior to access an object outside of its
674lifetime.</p>
675
676<p>A load or store with <q>primitive semantics</q> has the same
677semantics as the respective operation would have on an <tt>void*</tt>
678lvalue with the same alignment and non-ownership qualification.</p>
679
680<p><span class="term">Reading</span> occurs when performing a
681lvalue-to-rvalue conversion on an object lvalue.
682
683<ul>
684<li>For <tt>__weak</tt> objects, the current pointee is retained and
685then released at the end of the current full-expression. This must
686execute atomically with respect to assignments and to the final
687release of the pointee.</li>
688<li>For all other objects, the lvalue is loaded with primitive
689semantics.</li>
690</ul>
691</p>
692
693<p><span class="term">Assignment</span> occurs when evaluating
694an assignment operator. The semantics vary based on the qualification:
695<ul>
696<li>For <tt>__strong</tt> objects, the new pointee is first retained;
697second, the lvalue is loaded with primitive semantics; third, the new
698pointee is stored into the lvalue with primitive semantics; and
699finally, the old pointee is released. This is not performed
700atomically; external synchronization must be used to make this safe in
701the face of concurrent loads and stores.</li>
702<li>For <tt>__weak</tt> objects, the lvalue is updated to point to the
703new pointee, unless that object is currently undergoing deallocation,
704in which case it the lvalue is updated to a null pointer. This must
705execute atomically with respect to other assignments to the object, to
706reads from the object, and to the final release of the new pointed-to
707value.</li>
708<li>For <tt>__unsafe_unretained</tt> objects, the new pointee is
709stored into the lvalue using primitive semantics.</li>
710<li>For <tt>__autoreleasing</tt> objects, the new pointee is retained,
711autoreleased, 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
716lifetime begins, which depends on its storage duration.
717Initialization proceeds in two stages:
718<ol>
719<li>First, a null pointer is stored into the lvalue using primitive
720semantics. This step is skipped if the object
721is <tt>__unsafe_unretained</tt>.</li>
722<li>Second, if the object has an initializer, that expression is
723evaluated and then assigned into the object using the usual assignment
724semantics.</li>
725</ol>
726</p>
727
728<p><span class="term">Destruction</span> occurs when an object's
729lifetime ends. In all cases it is semantically equivalent to
730assigning a null pointer to the object, with the proviso that of
731course the object cannot be legally read after the object's lifetime
732ends.</p>
733
734<p><span class="term">Moving</span> occurs in specific situations
735where an lvalue is <q>moved from</q>, meaning that its current pointee
736will be used but the object may be left in a different (but still
737valid) state. This arises with <tt>__block</tt> variables and rvalue
738references in C++. For <tt>__strong</tt> lvalues, moving is equivalent
739to loading the lvalue with primitive semantics, writing a null pointer
740to it with primitive semantics, and then releasing the result of the
741load at the end of the current full-expression. For all other
742lvalues, 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>
753object of non-automatic storage duration.</p>
754
755<div class="rationale"><p>Rationale: autorelease pools are tied to the
756current thread and scope by their nature. While it is possible to
757have temporary objects whose instance variables are filled with
758autoreleased objects, there is no way that ARC can provide any sort of
759safety guarantee there.</p></div>
760
761<p>It is undefined behavior if a non-null pointer is assigned to
762an <tt>__autoreleasing</tt> object while an autorelease pool is in
763scope and then that object is read after the autorelease pool's scope
764is 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
772converted, explicitly or implicitly, to the type <tt>U*</tt>,
773where <tt>T</tt> and <tt>U</tt> have different ownership
774qualification, 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
785well-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
790Objective-C++.</p>
791
792<div class="rationale"><p>Rationale: these rules provide a reasonable
793level of type-safety for indirect pointers, as long as the underlying
794memory is not deallocated. The conversion to <tt>const
795__unsafe_unretained</tt> is permitted because the semantics of reads
796are equivalent across all these ownership semantics, and that's a very
797useful and common pattern. The interconversion with <tt>void*</tt> is
798useful for allocating memory or otherwise escaping the type system,
799but use it carefully. <tt>reinterpret_cast</tt> is considered to be
800an obvious enough sign of taking responsibility for any
801problems.</p></div>
802
803<p>It is undefined behavior to access an ownership-qualified object
804through an lvalue of a differently-qualified type, except that any
805non-<tt>__weak</tt> object may be read through
806an <tt>__unsafe_unretained</tt> lvalue.</p>
807
808<p>It is undefined behavior if a managed operation is performed on
809a <tt>__strong</tt> or <tt>__weak</tt> object without a guarantee that
810it contains a primitive zero bit-pattern, or if the storage for such
811an object is freed or reused without the object being first assigned a
812null pointer.</p>
813
814<div class="rationale"><p>Rationale: ARC cannot differentiate between
815an assignment operator which is intended to <q>initialize</q> dynamic
816memory and one which is intended to potentially replace a value.
817Therefore the object's pointer must be valid before letting ARC at it.
818Similarly, C and Objective-C do not provide any language hooks for
819destroying objects held in dynamic memory, so it is the programmer's
820responsibility to avoid leaks (<tt>__strong</tt> objects) and
821consistency errors (<tt>__weak</tt> objects).</p>
822
823<p>These requirements are followed automatically in Objective-C++ when
824creating objects of retainable object owner type with <tt>new</tt>
825or <tt>new[]</tt> and destroying them with <tt>delete</tt>,
826<tt>delete[]</tt>, or a pseudo-destructor expression. Note that
827arrays of nontrivially-ownership-qualified type are not ABI compatible
828with non-ARC code because the element type is non-POD: such arrays
829that are <tt>new[]</tt>'d in ARC translation units cannot
830be <tt>delete[]</tt>'d in non-ARC translation units and
831vice-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>,
840where <tt>oq</tt> is an ownership qualifier, then the argument is a
841candidate 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
846a <tt>U __strong *</tt>.</li>
847</ul>
848
849<p>For purposes of overload resolution, an implicit conversion
850sequence requiring a pass-by-writeback is always worse than an
851implicit conversion sequence not requiring a pass-by-writeback.</p>
852
853<p>The pass-by-writeback is ill-formed if the argument expression does
854not have a legal form:</p>
855
856<ul>
857<li><tt>&var</tt>, where <tt>var</tt> is a scalar variable of
858automatic storage duration with retainable object pointer type</li>
859<li>a conditional expression where the second and third operands are
860both 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
866the argument serves two purposes. First, it makes it impossible to
867pass the address of an array to the argument, which serves to protect
868against an otherwise serious risk of mis-inferring an <q>array</q>
869argument as an out-parameter. Second, it makes it much less likely
870that the user will see confusing aliasing problems due to the
871implementation, below, where their store to the writeback temporary is
872not 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
892convoluted. In an ideal world, we would see that a local variable is
893being passed to an out-parameter and retroactively modify its type to
894be <tt>__autoreleasing</tt> rather than <tt>__strong</tt>. This would
895be remarkably difficult and not always well-founded under the C type
896system. However, it was judged unacceptably invasive to require
897programmers to write <tt>__autoreleasing</tt> on all the variables
898they intend to use for out-parameters. This was the least bad
899solution.</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
907union to have a nontrivially ownership-qualified type.</p>
908
909<div class="rationale"><p>Rationale: the resulting type would be
910non-POD in the C++ sense, but C does not give us very good language
911tools for managing the lifetime of aggregates, so it is more
912convenient to simply forbid them. It is still possible to manage this
913with a <tt>void*</tt> or an <tt>__unsafe_unretained</tt>
914object.</p></div>
915
916<p>This restriction does not apply in Objective-C++. However,
917nontrivally ownership-qualified types are considered non-POD: in C++0x
918terms, they are not trivially default constructible, copy
919constructible, move constructible, copy assignable, move assignable,
920or destructible. It is a violation of C++ One Definition Rule to use
921a class outside of ARC that, under ARC, would have an
922ownership-qualified member.</p>
923
924<div class="rationale"><p>Rationale: unlike in C, we can express all
925the necessary ARC semantics for ownership-qualified subobjects as
926suboperations of the (default) special member functions for the class.
927These functions then become non-trivial. This has the non-obvious
928repercussion that the class will have a non-trivial copy constructor
929and non-trivial destructor; if it wouldn't outside of ARC, this means
930that objects of the type will be passed and returned in an
931ABI-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
944without an explicit ownership qualifier, its type is implicitly
945adjusted 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
949have <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,
958then:</p>
959
960<ul>
961<li>if <tt>T</tt> is <tt>const</tt>-qualified or <tt>Class</tt>, then
962it is implicitly qualified with <tt>__unsafe_unretained</tt>;</li>
963<li>otherwise, it is implicitly qualified
964with <tt>__autoreleasing</tt>.</li>
965</ul>
966</p>
967
968<div class="rationale"><p>Rationale: <tt>__autoreleasing</tt> exists
969mostly for this case, the Cocoa convention for out-parameters. Since
970a pointer to <tt>const</tt> is obviously not an out-parameter, we
971instead use a type more useful for passing arrays. If the user
972instead intends to pass in a <em>mutable</em> array, inferring
973<tt>__autoreleasing</tt> is the wrong thing to do; this directs some
974of 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
977general rule requiring ownership qualifiers.</p>
978
979<p>This rule does not apply in Objective-C++ if a parameter's type is
980dependent in a template pattern and is only <em>instantiated</em> to
981a type which would be a pointer to an unqualified retainable object
982pointer type. Such code is still ill-formed.</p>
983
984<div class="rationale"><p>Rationale: the convention is very unlikely
985to be intentional in template code.</p></div>
986
987</div> <!-- ownership.inference.indirect_parameters -->
Douglas Gregore559ca12011-06-17 22:11:49 +0000988
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
993retainable object owner type that does not have an explicit ownership
994qualifier, it is adjusted to have <tt>__strong</tt>
Douglas Gregor54fb28a2011-06-17 22:19:27 +0000995qualification. This adjustment occurs regardless of whether the
Douglas Gregore559ca12011-06-17 22:11:49 +0000996template 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&lt;id&gt;</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 McCall82467022011-06-15 21:21:53 +00001001</div> <!-- ownership.inference -->
1002</div> <!-- ownership -->
1003
Douglas Gregore559ca12011-06-17 22:11:49 +00001004
John McCall82467022011-06-15 21:21:53 +00001005<div id="family">
1006<h1>Method families</h1>
1007
1008<p>An Objective-C method may fall into a <span class="term">method
1009family</span>, which is a conventional set of behaviors ascribed to it
1010by 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
1023underscores, the first component of the selector either consists
1024entirely of the name of the method family or it begins with that name
1025followed by a character other than a lowercase letter. For
1026example, <tt>_perform:with:</tt> and <tt>performWith:</tt> would fall
1027into the <tt>perform</tt> family (if we recognized one),
1028but <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
1038Objective-C pointer type. Additionally, a program is ill-formed if it
1039declares or contains a call to an <tt>init</tt> method whose return
1040type is neither <tt>id</tt> nor a pointer to a super-class or
1041sub-class of either the declaring class, if the method was declared on
1042a class, or the static receiver type of the call, if it was declared
1043on a protocol.</p>
1044
1045<div class="rationale"><p>Rationale: there are a fair number of existing
1046methods with <tt>init</tt>-like selectors which nonetheless don't
1047follow the <tt>init</tt> conventions. Typically these are either
1048accidental naming collisions or helper methods called during
1049initialization. Because of the peculiar retain/release behavior
1050of <tt>init</tt> methods, it's very important not to treat these
1051methods as <tt>init</tt> methods if they aren't meant to be. It was
1052felt that implicitly defining these methods out of the family based on
1053the exact relationship between the return type and the declaring class
1054would much too subtle and fragile. Therefore we identify a small
1055number of legitimate-seeming return types and call everything else an
1056error. This serves the secondary purpose of encouraging programmers
1057not 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,
1062implementations, and overrides do not all have the same method
1063family.</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>
1069attribute to precisely control which method family it belongs to. If
1070a method in an <tt>@implementation</tt> does not have this attribute,
1071but there is a method declared in the corresponding <tt>@interface</tt>
1072that does, then the attribute is copied to the declaration in the
1073<tt>@implementation</tt>. The attribute is available outside of ARC,
1074and 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>.
1079If <i>family</i> is <tt>none</tt>, the method has no family, even if
1080it would otherwise be considered to have one based on its selector and
1081type. Otherwise, <i>family</i> must be one
1082of <tt>alloc</tt>, <tt>copy</tt>, <tt>init</tt>,
1083<tt>mutableCopy</tt>, or <tt>new</tt>, in which case the method is
1084considered to belong to the corresponding family regardless of its
1085selector. It is an error if a method that is explicitly added to a
1086family in this way does not meet the requirements of the family other
1087than the selector naming convention.</p>
1088
1089<div class="rationale"><p>Rationale: the rules codified in this document
1090describe the standard conventions of Objective-C. However, as these
1091conventions have not heretofore been enforced by an unforgiving
1092mechanical system, they are only imperfectly kept, especially as they
1093haven't always even been precisely defined. While it is possible to
1094define low-level ownership semantics with attributes like
1095<tt>ns_returns_retained</tt>, this attribute allows the user to
1096communicate semantic intent, which of use both to ARC (which, e.g.,
1097treats 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
1104semantics for its parameters and return type.</p>
1105
1106<p>Methods in the <tt>alloc</tt>, <tt>copy</tt>, <tt>mutableCopy</tt>,
1107and <tt>new</tt> families &mdash; that is, methods in all the
1108currently-defined families except <tt>init</tt> &mdash; transfer
1109ownership of a +1 retain count on their return value to the calling
1110function, as if they were implicitly annotated with
1111the <tt>ns_returns_retained</tt> attribute. However, this is not true
1112if 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
1119of a +1 retain count on their <tt>self</tt> parameter, exactly as if
1120the method had the <tt>ns_consumes_self</tt> attribute, and must
1121transfer ownership of a +1 retain count on their return value, exactly
1122as if they method had the <tt>ns_returns_retained</tt> attribute.
1123Neither 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
1127called a <span class="term">delegate init call</span>. It is an error
1128for a delegate init call to be made except from an <tt>init</tt>
1129method, and excluding blocks within such methods.</p>
1130
1131<p>The variable <tt>self</tt> is mutable in an <tt>init</tt> method
1132and is implicitly qualified as <tt>__strong</tt>. However, a program
1133is ill-formed, no diagnostic required, if it alters <tt>self</tt>
1134except to assign it the immediate result of a delegate init call. It
1135is an error to use the previous value of <tt>self</tt> after the
1136completion of a delegate init call.</p>
1137
1138<p>A program is ill-formed, no diagnostic required, if it causes two
1139or more calls to <tt>init</tt> methods on the same object, except that
1140each <tt>init</tt> method invocation may perform at most one
1141delegate init call.</p>
1142
John McCallf3d08a62011-06-18 07:31:30 +00001143</div> <!-- family.semantics.init -->
John McCall82467022011-06-15 21:21:53 +00001144
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
1149result 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
1158protocol-qualified <tt>id</tt>, or a type equal to the declaring class
1159or a superclass, then it is said to have a related result type. In
1160this case, when invoked in an explicit message send, it is assumed to
1161return 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
1165name <tt>T</tt>, the message send expression has type <tt>T*</tt>;
1166otherwise</li>
1167<li>if it is an instance method, and the receiver has type <tt>T</tt>,
1168the message send expression has type <tt>T</tt>; otherwise</li>
1169<li>the message send expression has the normal result type of the
1170method.</li>
1171</ul>
1172
1173<p>This is a new rule of the Objective-C language and applies outside
1174of ARC.</p>
1175
1176<div class="rationale"><p>Rationale: ARC's automatic code emission is
1177more prone than most code to signature errors, i.e. errors where a
1178call was emitted against one method signature, but the implementing
1179method has an incompatible signature. Having more precise type
1180information helps drastically lower this risks, as well as catching
1181a 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
1191behavior. These rules are based around a core assumption of
1192<span class="term">local balancing</span>: that other code will
1193perform retains and releases as necessary (and only as necessary) for
1194its own safety, and so the optimizer does not need to consider global
1195properties of the retain and release sequence. For example, if a
1196retain and release immediately bracket a call, the optimizer can
1197delete the retain and release on the assumption that the called
1198function will not do a constant number of unmotivated releases
1199followed by a constant number of <q>balancing</q> retains, such that
1200the local retain/release pair is the only thing preventing the called
1201function from ending up with a dangling reference.</p>
1202
1203<p>The optimizer assumes that when a new value enters local control,
1204e.g. from a load of a non-local object or as the result of a function
1205call, it is instaneously valid. Subsequently, a retain and release of
1206a value are necessary on a computation path only if there is a use of
1207that value before the release and after any operation which might
1208cause a release of the value (including indirectly or non-locally),
1209and only if the value is not demonstrably already retained.</p>
1210
1211<p>The complete optimization rules are quite complicated, but it would
1212still be useful to document them here.</p>
1213
1214</div>
1215
1216<div id="misc">
1217<h1>Miscellaneous</h1>
1218
John McCallf3d08a62011-06-18 07:31:30 +00001219<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
1226send, or <tt>@selector</tt> expression for any of the following
1227selectors:
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
1237because ARC robs it of consistent semantics. The others were banned
1238after 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
1241naively or accidentally tried to incorporate code written for manual
1242retain/release code into an ARC program. At best, such code would do
1243twice as much work as necessary; quite frequently, however, ARC and
1244the explicit code would both try to balance the same retain, leading
1245to crashes. The cost is losing the ability to perform <q>unrooted</q>
1246retains, i.e. retains not logically corresponding to a strong
1247reference in the object graph.</p>
1248
1249<p><b>Ignoring</b> them would badly violate user expectations about their
1250code. While it <em>would</em> make it easier to develop code simultaneously
1251for ARC and non-ARC, there is very little reason to do so except for
1252certain library developers. ARC and non-ARC translation units share
1253an execution model and can seamlessly interoperate. Within a
1254translation unit, a developer who faithfully maintains their code in
1255non-ARC mode is suffering all the restrictions of ARC for zero
1256benefit, while a developer who isn't testing the non-ARC mode is
1257likely 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
1260to migrate existing code to ARC. The best answer to that, given a
1261number of other changes and restrictions in ARC, is to provide a
1262specialized tool to assist users in that migration.</p>
1263
1264<p>Implementing these methods was banned because they are too integral
1265to the semantics of ARC; many tricks which worked tolerably under
1266manual reference counting will misbehave if ARC performs an ephemeral
1267extra retain or two. If absolutely required, it is still possible to
1268implement them in non-ARC code, for example in a category; the
1269implementations must obey the <a href="#objects.retains">semantics</a>
1270laid 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
1279or <tt>@selector</tt> expression for the selector <tt>dealloc</tt>.</p>
1280
1281<div class="rationale"><p>Rationale: there are no legitimate reasons
1282to call <tt>dealloc</tt> directly.</p></div>
1283
1284<p>A class may provide a method definition for an instance method
1285named <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
1287its instance variables are destroyed. The superclass's implementation
1288of <tt>dealloc</tt> will be called automatically when the method
1289returns.</p>
1290
1291<div class="rationale"><p>Rationale: even though ARC destroys instance
1292variables automatically, there are still legitimate reasons to write
1293a <tt>dealloc</tt> method, such as freeing non-retainable resources.
1294Failing to call <tt>[super&nbsp;dealloc]</tt> in such a method is nearly
1295always a bug. Sometimes, the object is simply trying to prevent
1296itself from being destroyed, but <tt>dealloc</tt> is really far too
1297late for the object to be raising such objections. Somewhat more
1298legitimately, an object may have been pool-allocated and should not be
1299deallocated with <tt>free</tt>; for now, this can only be supported
1300with a <tt>dealloc</tt> implementation outside of ARC. Such an
1301implementation must be very careful to do all the other work
1302that <tt>NSObject</tt>'s <tt>dealloc</tt> would, which is outside the
1303scope of this document to describe.</p></div>
1304
1305</div>
1306
1307</div> <!-- misc.special_methods -->
1308
John McCall82467022011-06-15 21:21:53 +00001309<div id="autoreleasepool">
1310<h1><tt>@autoreleasepool</tt></h1>
1311
1312<p>To simplify the use of autorelease pools, and to bring them under
1313the control of the compiler, a new kind of statement is available in
1314Objective-C. It is written <tt>@autoreleasepool</tt> followed by
1315a <i>compound-statement</i>, i.e. by a new scope delimited by curly
1316braces. Upon entry to this block, the current state of the
1317autorelease pool is captured. When the block is exited normally,
1318whether by fallthrough or directed control flow (such
1319as <tt>return</tt> or <tt>break</tt>), the autorelease pool is
1320restored to the saved state, releasing all the objects in it. When
1321the block is exited with an exception, the pool is not drained.</p>
1322
John McCallf3d08a62011-06-18 07:31:30 +00001323<p><tt>@autoreleasepool</tt> may be used in non-ARC translation units,
1324with equivalent semantics.</p>
1325
John McCall82467022011-06-15 21:21:53 +00001326<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
1330important for the compiler to reason about, but it is far too much to
1331expect the compiler to accurately reason about control dependencies
1332between two calls. It is also very easy to accidentally forget to
1333drain an autorelease pool when using the manual API, and this can
1334significantly inflate the process's high-water-mark. The introduction
1335of a new scope is unfortunate but basically required for sane
1336interaction with the rest of the language. Not draining the pool
1337during an unwind is apparently required by the Objective-C exceptions
1338implementation.</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
1346never actually retained by the implementation. It is undefined
1347behavior, or at least dangerous, to cause an object to be deallocated
1348during a message send to that object. To make this
1349safe, <tt>self</tt> is implicitly <tt>const</tt> unless the method is
1350in the <a href="#family.semantics.init"><tt>init</tt> family</a>.</p>
1351
1352<div class="rationale"><p>Rationale: the cost of
1353retaining <tt>self</tt> in all methods was found to be prohibitive, as
1354it tends to be live across calls, preventing the optimizer from
1355proving that the retain and release are unnecessary &mdash; for good
1356reason, as it's quite possible in theory to cause an object to be
1357deallocated during its execution without this retain and release.
1358Since it's extremely uncommon to actually do so, even unintentionally,
1359and since there's no natural way for the programmer to remove this
1360retain/release pair otherwise (as there is for other parameters by,
1361say, making the variable <tt>__unsafe_unretained</tt>), we chose to
1362make this optimizing assumption and shift some amount of risk to the
1363user.</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
1371enumeration loop, and the variable has no explicit ownership
1372qualifier, then it is qualified with <tt>const __strong</tt> and
1373objects encountered during the enumeration are not actually
1374retained.</p>
1375
1376<div class="rationale"><p>Rationale: this is an optimization made
1377possible because fast enumeration loops promise to keep the objects
1378retained during enumeration, and the collection itself cannot be
1379synchronously modified. It can be overridden by explicitly qualifying
1380the variable with <tt>__strong</tt>, which will make the variable
1381mutable again and cause the loop to retain the objects it
1382encounters.</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
1390evaluating a block literal expression have the same ownership
1391semantics as the local variables they capture. The capture is
1392performed by reading from the captured variable and initializing the
1393capture variable with that value; the capture variable is destroyed
1394when 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
1397equally to <tt>__block</tt> variables, which is a shift in semantics
1398from non-ARC, where <tt>__block</tt> variables did not implicitly
1399retain during capture.</p>
1400
1401<p><tt>__block</tt> variables of retainable object owner type are
1402moved off the stack by initializing the heap copy with the result of
1403moving from the stack copy.</tt></p>
1404
1405<p>With the exception of retains done as part of initializing
1406a <tt>__strong</tt> parameter variable or reading a <tt>__weak</tt>
1407variable, whenever these semantics call for retaining a value of
1408block-pointer type, it has the effect of a <tt>Block_copy</tt>. The
1409optimizer may remove such copies when it sees that the result is
1410used 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
1418releases:
1419<ul>
1420<li>It does not end the lifetime of <tt>__strong</tt> variables when
1421their scopes are abnormally terminated by an exception.</li>
1422<li>It does not perform releases which would occur at the end of
1423a 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
1428option <tt>-fno-objc-arc-exceptions</tt> to explicitly disable them,
1429with the last such argument <q>winning</q>.</p>
1430
1431<div class="rationale"><p>Rationale: the standard Cocoa convention is
1432that exceptions signal programmer error and are not intended to be
1433recovered from. Making code exceptions-safe by default would impose
1434severe runtime and code size penalties on code that typically does not
1435actually care about exceptions safety. Therefore, ARC-generated code
1436leaks by default on exceptions, which is just fine if the process is
1437going to be immediately terminated anyway. Programs which do care
1438about recovering from exceptions should enable the option.</p></div>
1439
1440<p>In Objective-C++, <tt>-fobjc-arc-exceptions</tt> is enabled by
1441default.</p>
1442
1443<div class="rationale"><p>Rationale: C++ already introduces pervasive
1444exceptions-cleanup code of the sort that ARC introduces. C++
1445programmers who have not already disabled exceptions are much more
1446likely to actual require exception-safety.</p></div>
1447
1448<p>ARC does end the lifetimes of <tt>__weak</tt> objects when an
1449exception terminates their scope unless exceptions are disabled in the
1450compiler.</p>
1451
1452<div class="rationale"><p>Rationale: the consequence of a
1453local <tt>__weak</tt> object not being destroyed is very likely to be
1454corruption of the Objective-C runtime, so we want to be safer here.
1455Of course, potentially massive leaks are about as likely to take down
1456the process as this corruption is if the program does try to recover
1457from exceptions.</p></div>
1458
1459</div> <!-- misc.exceptions -->
1460
1461</div> <!-- misc -->
1462</div> <!-- root -->
1463</body>
1464</html>