Only objects require memory management, and the basic data type is not required.
Description of the release, retain, and Retaincount methods for the object:
Release reference count-1
The retain reference count of +1 returns the object itself. The returned type is instancetype
Retaincount returns the number of reference counts
When the reference count is 0, the object's Dealloc method is called, so a reference to the member Property object needs to be freed in Dealloc.
In the Dealloc method of the subclass , you need to release the members of the parent class, so [Super Dealloc];
Use of retain and release
1. When an object alloc, new, [Mutable]copy is allocated memory, so after the use is complete, release. Similar to [person release]
2. When the object's retain method is called, the release is required after the object is used
3. In the Set method, a similar-(void) Setcup: (cup*) Cup; If you are different from an internal member, you need to first release the old value and then retain the parameter:
-(void) Setcup: (cup*) Cup {
if (cup! = _cup) {
[_cup release];
_cup = [Cup retain];
}
}
Finally, when the object holding the cup attribute is destroyed, the [_cup release] is called in the object's Dealloc method;
About Autoreleasepool
is an automatic object reference release pool, earlier, using NSAutoreleasePool to create a pool object and then call [Pool Addobject:obj] to add it to the pool.
The pool will automatically send a release message after the object is not used. Add the same object to the pool multiple times, and finally send the release multiple times.
Autorelease method
cup* cup = [[[Cup alloc] init] autorelease];
Inside is actually called the AddObject method of NSAutoreleasePool. will automatically release
--------------------------------------------------------------------------------------------------------------- ---------
The above is manual memory management
The following are automatic memory management
iOS5.0 (LLVM3.0) introduces ARC (automatic reference counting), which can automatically manage object references. Automatic retain, automatic release.
Obj-c: (ARC) Considerations for using Arc
1. Arc does not resolve non-objective-c memory management, such as malloc (), requires free ()
2. Arc minimum support for Xcode 4.2 and IOS4
3. Convert existing code to ARC by (Edit > Refactor > Convert to Objective-c arc)
4. Arc can no longer use retain, release, Retaincount, Autorelease, NSAutoreleasePool (with
@autoreleasepool code block overrides)
5. Arc cannot invoke [Super Dealloc] in Dealloc (), Dealloc () can still be used to remove registered agents or notifications, etc.
6. Arc cannot handle cases where a C struct contains an object pointer, and the OBJECTIVE-C class is applied to encapsulate it.
7. Attribute names may not start with new
8. Arc can be compatible with obj-c++
9. Cannot use "new" to start the property name (if used will have the following compilation error "Properties ' s synthesized getter follows COCOA naming convention for returning ' owned ' obj ECTs ")
Between the ID and void * If cast requires a specific method (__bridge keyword)
11. Cannot use Nsallocateobject, NSDEALLOCATEOBJEC
12. Do not overload the Dealloc (the function can be overloaded if it is disposed of outside the object's memory, but cannot be called [Super Dealloc])
When arc loads a non-arc file, it finds the appropriate file in the Targets->build phases->compile sources add field:
-fno-objc-arc
@autoreleasepool code block, an object in a block of code that, at the end of the block, sends a release message to all objects inside.
Code blocks can be nested.
OC Language Learning (vii) memory management