In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand autorelease in Objective". In the operation of actual cases, many people will encounter such a dilemma. Next, let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
If you really understand autorelease, then you understand the memory management of Objective c. Autorelease actually delays the call to release. For each Autorelease, the system simply puts the Object into the current Autorelease pool, and when the pool is released, all Object in that pool will be called Release.
[1] in the Iphone project, you will see a default Autorelease pool, which is created at the beginning of the program and destroyed when the program exits. According to the understanding of Autorelease, isn't it true that all objects in autorelease pool are only release when the program exits, which is different from a memory leak?
The answer is that for each Runloop, the system will implicitly create an Autorelease pool, so that all release pool will form 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 every Object in this pool will be release.
So what is a Runloop? A UI event, Timer call, delegate call, will all be a new Runloop. Examples are as follows:
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 call, globalObject is added as the current AutoreleaePool. }
[2] Why do you need Auto release?
2.1Many programmers who turn around with auto release will say, what's so good about this auto relase? just like it is, if you apply for it and release it, you can't control it at all. You don't know when it will be really release. I understand that one of its functions is to make each function responsible for its own application object, apply and release it on its own. The caller of the function does not need to care about the management of its internal application object. In the following example, the caller of Func1 no longer needs to care about the release of obj.
ClassA * Func1 () {ClassA * obj = [ClassA alloc] init] autorelease]; return obj;}
In fact, the objects returned by constructors like [NSString stringWithFormat:] are all autorelease.
2. 2) autorelease pool to avoid frequent requests / releases of memory (that's what pool does). This should be relatively easy to understand.
Summary: 1) be sure to pay attention to the life cycle of Autorelease pool, understand Runloop, and avoid using it after the object is released.
2) [NSString stringWithFormat:] the object returned by this kind of function does not need to release itself. It has already been autorelease. If you want to use it as a global object, you must retain it yourself and release it when you release it.
This is the end of the content of "how to understand autorelease in Objective". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.