What is the mechanism of ARC? Where does it put the retain/release function call?
Please stop thinking about these issues and focus more on the following issues, such as your program logic, strong object, weak reference, and object affiliation, possible issues such as loop references.
Do I still need to write a dealloc function for my class?
Possible.
Because ARC does not automatically call malloc/free and does not automatically manage lifecycles such as core function objects and file descriptors, you still need to release them in dealloc.
You will not (actually cannot) release the object instance, but you still need to call [self setDelegate: nil] for the system class or other ARC will not automatically release the code to release the resource.
The dealloc method is not required in ARC and cannot call [super dealloc].
Will circular references appear in ARC?
Yes.
ARC automatically generates the retain/release function and inherits the mechanism for generating circular references. Fortunately, ARC seldom has memory leakage because it determines whether to use retain when declaring attribute variables.
Note: Search for retain cycles.
How does the block code under ARC work?
Block Code can only work in the method passed on the stack in the ARC mode, for example, in the return statement. You do not need to call Block Copy. You must still use the [^ {} copy] and other retain functions when passing in the stack for arrayWithObjects.
One thing to mention is that in the ARC mode, __block NSString * S is usable, and it is not a wild pointer. Use _ block _ unsafe_unretained NSString * S or (a better way) _ block _ weak NSString * S.
Can I create an instance array in a project that supports ARC?
Yes, you can, as shown in the following example:
// Note calloc () to get zero-filled memory. _ strong SomeClass ** dynamicArray = (_ strong SomeClass **) calloc (sizeof (SomeClass *), entries); for (int I = 0; I <entries; I ++) {dynamicArray [I] = [[SomeClass alloc] init];} // When you're done, set each entry to nil to tell ARC to release the object. for (int I = 0; I <entries; I ++) {dynamicArray [I] = nil;} free (dynamicArray );
Note the following points:
You must write _ strong SomeClass ** in some cases, because if you do not write it, the default value is _ autoreleasing SomeClass **.
The Applied memory must be filled with 0.
Before releasing this array, you must set every element to nil (call memset and input 0 is useless ).
You must avoid using memcpy or realloc.
Is ARC slow?
It depends on how you measure the problem, but generally the answer to this question is "no ". The following is an explanation of the original article. I want to believe in the ability of the compiler in this aspect.
It depends on what you're measuring, but generally "no. "The compiler efficiently eliminates extends extraneousretain/release CILS and much effort has been invested in speeding up the Objective-C runtime in general. in particle, the common "return a retain/autoreleased object" pattern is much faster and does not actually put the object into the autorelease pool, when the caller of the method is ARC code.
One issue to be aware of is that the optimizer is not run in common debug deployments, so many CT to see a lot more retain/release traffic at-O0 than at-OS.
Can ARC work in the ObjC ++ mode?
You can even use strong/weak id in the class or container. To work properly, ARC adds retain/release logic to the copy constructor and destructor during compilation. One thing to avoid is that you cannot use _ strong for some pointers, for example:
Std: vector <__strong NSString *> V;
Which classes do not support automatic nil-based weak references?
Instances of the following classes cannot be used.Automatic nil-based weak references
NSATSTypesetter, NSColorSpace, NSFont, NSFontManager, NSFontPanel, NSImage, NSMenuView, NSParagraphStyle, border, NSTableCellView, NSTextView, NSViewController, NSWindow, andNSWindowController. in addition, all classes in the AV Foundation framework on OS X are not supported.Automatic nil-based weak references.
If the attribute variables are instances of these classes, use assign to replace weak; as the variable, use _ unsafe_unretained to replace _ weak.
In addition, you do not need to make weak applications for NSHashTable, NSMapTable, and NSPointerArray instances.
When I write a subclass that uses NSCopyOjbect like NSCell, do I need to pay special attention to it?
There is nothing special, and ARC will do everything.
Can I specify whether to use ARC for each file separately?
Yes.
When you add ARC to an old project, the compilation option-fobjc-arc is valid for all files. You can use the compile option-fno-objc-arc to disable ARC for a class. On the Build Phases page on the target, open the Compile Sources group. There is a file list. Double-click a file and add-fno-objc-arc to disable ARC for the file.