Objective-C memoryManagement ComprehensionAutoreleaseIs the content to be introduced in this article, if you can really understandAutoreleaseSo you understandObjective-COfMemoryManagement.AutoreleaseThe call to release is actually delayed.AutoreleaseThe system only places the Object in the currentAutoreleaseWhen the pool is released, all objects in the pool are called Release.
1. In the Iphone project, you will see a defaultAutoreleasePool, which is created when the program starts and destroyed when the program exits.AutoreleaseNot allAutoreleaseObjects in the pool are release only when the program exits.MemoryWhat is the difference between leaks?
The answer is that for each Runloop, the system will implicitly create an Autorelease pool, so that all release pools will constitute a stack structure like CallStack. At the end of each Runloop, the Autorelease pool at the top of the current stack will be destroyed, so that each Object in the pool will be release.
So what is a Runloop? A ui event, Timer call and delegate call, will all be a new Runloop. Example:
- NSString * globalObject;
- -(Void) applicationDidFinishLaunching :( UIApplication *) application
- {
- GlobalObject = [[NSString alloc] initWithFormat: @ "Test"];
- NSLog (@ "Retain count after create: % d", [globalObject retainCount]); // output 1.
- [GlobalObject retain];
- NSLog (@ "Retain count after retain: % d", [globalObject retainCount]); // output 2.
- }
- -(Void) applicationWillTerminate :( UIApplication *) application
- {
- NSLog (@ "Retain count after Button click runloop finished: % d", [globalObject retainCount]);
- // Output 1. Button click loop finished, it's autorelease pool released, globalObject get released once.
- }
- -(IBAction) onButtonClicked
- {
- [GlobalObject autorelease];
- NSLog (@ "Retain count after autorelease: % d", [globalObject retainCount]);
- // Output 2. Autorelease is called, and globalObject is added as the current AutoreleaePool.
- }
2. Why do I need Auto release?
1) Many C/C ++ programmers will say that this auto release is as good as C/C ++, the auto relase is totally uncontrollable. You don't know when it will be actually release. In my understanding, each function is responsible for the objects applied for, applied for, and released by itself. The caller of this function does not need to care about the management of internal application objects. In the following example, the caller of Func1 does not need to care about the release of obj.
- ClassA *Func1()
- {
- ClassA *obj = [[[ClassA alloc]init]autorelease];
- return obj;
- }
In fact, all objects returned by constructors such as [NSString stringWithFormat:] are autorelisted.
Autorelease pool is used to avoid frequent application/release of memory ). This should be relatively easy to understand.
Conclusion: 1) Be sure to pay attention to the lifecycle of the Autorelease pool and understand the Runloop to avoid using it after the object is released.
2) [NSString stringWithFormat:] objects returned by such functions do not need to be release by themselves.AutoreleaseIf you want to use it as a global object, you must retain it and release it again.
Summary:Objective-C memoryManagement ComprehensionAutoreleaseI hope this article will help you!