14
14
font-style : italic
15
15
}
16
16
17
+ div .rationale em {
18
+ font-style : normal
19
+ }
20
+
17
21
div h1 { font-size : 2em ; margin : .67em 0 }
18
22
div div h1 { font-size : 1.5em ; margin : .75em 0 }
19
23
div div div h1 { font-size : 1.17em ; margin : .83em 0 }
@@ -1136,7 +1140,7 @@ <h1>Semantics of <tt>init</tt></h1>
1136
1140
each < tt > init</ tt > method invocation may perform at most one
1137
1141
delegate init call.</ p >
1138
1142
1139
- </ div > <!-- family.semantics.delegate- init -->
1143
+ </ div > <!-- family.semantics.init -->
1140
1144
1141
1145
< div id ="family.semantics.result_type ">
1142
1146
< h1 > Related result types</ h1 >
@@ -1212,6 +1216,96 @@ <h1>Optimization</h1>
1212
1216
< div id ="misc ">
1213
1217
< h1 > Miscellaneous</ h1 >
1214
1218
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
+
1215
1309
< div id ="autoreleasepool ">
1216
1310
< h1 > < tt > @autoreleasepool</ tt > </ h1 >
1217
1311
@@ -1226,6 +1320,9 @@ <h1><tt>@autoreleasepool</tt></h1>
1226
1320
restored to the saved state, releasing all the objects in it. When
1227
1321
the block is exited with an exception, the pool is not drained.</ p >
1228
1322
1323
+ < p > < tt > @autoreleasepool</ tt > may be used in non-ARC translation units,
1324
+ with equivalent semantics.</ p >
1325
+
1229
1326
< p > A program is ill-formed if it refers to the
1230
1327
< tt > NSAutoreleasePool</ tt > class.</ p >
1231
1328
0 commit comments