Objective-C memory management understanding Autorelease

Source: Internet
Author: User

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:

 
 
  1. NSString * globalObject;
  2. -(Void) applicationDidFinishLaunching :( UIApplication *) application
  3. {
  4. GlobalObject = [[NSString alloc] initWithFormat: @ "Test"];
  5. NSLog (@ "Retain count after create: % d", [globalObject retainCount]); // output 1.
  6. [GlobalObject retain];
  7. NSLog (@ "Retain count after retain: % d", [globalObject retainCount]); // output 2.
  8. }
  9. -(Void) applicationWillTerminate :( UIApplication *) application
  10. {
  11. NSLog (@ "Retain count after Button click runloop finished: % d", [globalObject retainCount]);
  12. // Output 1. Button click loop finished, it's autorelease pool released, globalObject get released once.
  13. }
  14. -(IBAction) onButtonClicked
  15. {
  16. [GlobalObject autorelease];
  17. NSLog (@ "Retain count after autorelease: % d", [globalObject retainCount]);
  18. // Output 2. Autorelease is called, and globalObject is added as the current AutoreleaePool.
  19. }

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.

 
 
  1. ClassA *Func1()  
  2. {  
  3.   ClassA *obj = [[[ClassA alloc]init]autorelease];  
  4.   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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.