IOS in ARC environment Dealloc use, understanding misunderstanding

Source: Internet
Author: User
Tags uikit

IOS in ARC environment Dealloc use, understanding misunderstanding

The beautiful Life of the Sun Vulcan (http://blog.csdn.net/opengl_es)

This article follows "Attribution-non-commercial use-consistent" authoring public agreement

Reprint Please keep this sentence: Sun Vulcan's Beautiful Life-this blog focuses on Agile development and mobile and IoT device research: IOS, Android, HTML5, Arduino, Pcduino , Otherwise, the article from this blog refused to reprint or reprint. Thank you for your cooperation.


Last word. I've been misunderstood since I contacted ARC:

In the ARC environment, the overloaded Dealloc method is called as well. Simply, the method of the parent class cannot be called in the implementation of the method.

Here's a demonstration sample to verify:

A test class test, create and destroy it, see if Dealloc is called in the ARC environment, and the second is to invoke the implementation of the parent class in Dealloc to see what happens.

Another is the view controller, which is used to add two buttons, in which the event method of a button is used to create the Test class. There's another one for release, and here's the same test in an ARC when an object is pointed to by 0 references. is released immediately.


The declaration of the Test class is as follows:

#import <Foundation/Foundation.h> @interface Test:nsobject@end


The Declaration and implementation of the Test class are as follows:

#import "Test.h" @implementation test-(ID) init {self            = [super init];    if (self) {                    }    return to self;} -(void) Dealloc {        NSLog (@ "Dealloc");    [Super Dealloc];} @end


View Controller declaration:
#import <UIKit/UIKit.h> #import "Test.h" @interface Viewcontroller:uiviewcontroller@property (nonatomic, strong ) Test *test; @end



View Controller implementation:
#import "ViewController.h" @interface Viewcontroller () @end @implementation viewcontroller-(void) viewdidload{    [ Super viewdidload];//do any additional setup after loading the view, typically from a nib.} -(void) didreceivememorywarning{    [Super didreceivememorywarning];    Dispose of any resources the can be recreated.} -(Ibaction) generatebuttonpressed: (UIButton *) Sender {        self.test = [[Test alloc] init];} -(Ibaction) removebuttonpressed: (UIButton *) Sender {        self.test = nil;} @end

Tested. Everything is fine, and when you press the release button, the Dealloc method of the object of the Test class is called immediately. Basically did not feel the delay.

In addition, when the Dealloc method implementation of the parent class is finally called at the end of the Dealloc method of the Test class, XCode 5.1.1 prompts itself voluntarily. For example, with:

ARC refuses to send an explicit ' dealloc ' message.



So far. Make up your mind to sort out the ARC-related documentation, and take the time out of business every day and read it through, which is very necessary.

ARC for me. It's just about understanding. On the one hand does not have the time, also has the aspect ego to feel good, thinks understands and grasps the Apple architecture design idea. Click here to keep guessing. Finally make a joke of today.

Everything, or from the official information to verify the good, but also to avoid the time of the uncertainty and distraction.


"Good at computing" needs to be rigorous first, and "calculating" is not too rigorous. Rough is not bound.


Discuss
Discussion

A possibly message for that object generates an error message. This information indicates that the message was sent to a freed object (which provides the freed memory that has not been reused).


Subsequent messages to the receiver could generate an error indicating that a message is sent to a deallocated object (Prov IDed the deallocated memory hasn ' t been reused yet).

You overload the method for processing resources. Instead of the instance variable that is used to process the object. Like what:
You override this method to dispose of resources other than the object ' s instance variables, for example:

-(void) Dealloc {free    (mybigblockofmemory);}


In the implementation of Dealloc, do not invoke the implementation of the parent class.

You should try to avoid it. Use Dealloc to manage the life cycle of limited resources such as file descriptive descriptors.


In a implementation of Dealloc, do not invoke the superclass ' s implementation. You should try to avoid managing the lifetime of limited resources such as file descriptors usingdealloc.

Never send a dealloc message directly.

Conversely, no matter what the object's Dealloc method is called by execution. See the Advanced Memory Management Programming Guide for more specific content.
You never send a dealloc message directly. Instead, an object ' sdealloc method is invoked by the runtime. See AdvancedMemory Management Programming Guide for more details.

Special precautions
Special considerations

When ARC is not used, your DEALLOC implementation must call the implementation of the parent class as the last instruction. (The implied meaning is.) The implementation of the parent class cannot be called when using ARC)
When is not using ARC, your implementation of Dealloc must invoke the superclass ' s implementation as it last Instru Ction.







IOS in ARC environment Dealloc use, understanding misunderstanding

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.