The release time of Autorelease in Objective-c

Source: Internet
Author: User

If you have used mrr,autorelease this keyword should be too familiar, each time we generate a new object return, we need to send an autorelease message to this object, in order to delay the release of the created object. When is it that the object will be released? Is there any way to release a Autorelease object faster?

Let's start by looking at a phenomenon:

@property (Weak, nonatomic) NSString *weakstring;- (void) viewdidload {[Super viewdidload]; //Do any additional setup after loading the view.NSString*hello = [[NSString alloc] initwithcstring:"It is released at the end of current Runloop"encoding:nsutf8stringencoding]; Self.weakstring=Hello; NSLog (@"%@ - %@", Self.weakstring, Nsstringfromselector (_cmd));}- (void) Viewwillappear: (BOOL) animated{[Super viewwillappear:animated]; NSLog (@"%@ - %@", Self.weakstring, Nsstringfromselector (_cmd));}- (void) Viewdidappear: (BOOL) animated{[Super viewdidappear:animated]; NSLog (@"%@ - %@", Self.weakstring, Nsstringfromselector (_cmd)); Dispatch_async (Dispatch_get_main_queue (),^{NSLog (@"-2--%@-%@", Self.weakstring, Nsstringfromselector (_cmd)); });}

We created a Viewdidload method that creates a weak pointer to a string, and when the code executes to Viewwillappear: and viewdidappear: We can still print the string that the weak pointer points to, When you perform a print operation in Viewdidappear: In the next runloop, the weak pointer points to nil, why?

We can completely translate this statement to MRR:self.weakString = Hello

self.weakstring = [[Hello retain] autorelease];

So, can we guess that Autorelease's object was released after the current Runloop ended?

There are a few things that we need to clarify:

1) autorelease Pool

The release time of Autorelease in Objective-c

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.