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 re-reproduced, thank you for your cooperation.


Finally, I have been misunderstood since I contacted ARC:

In the ARC environment, the overloaded Dealloc method is called, except that the method of the parent class cannot be called in the implementation of the method.

Let's look at an example to verify:

A class test to be tested, 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.

The other is the view controller, which is used to add two buttons, one of which is the event method for creating the test class and the other for releasing, and here the same tests whether an ARC is released immediately when an object is pointed to by 0 references.


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

After testing, everything is fine, and when the release button is pressed, the Dealloc method of the object of the test class is immediately called, with little sense of delay.

In addition, when the Dealloc method implementation of the parent class is called at the end of the Dealloc method of the Test class, XCode 5.1.1 automatically prompts, such as:

ARC refuses to send an explicit ' dealloc ' message.



At this point, make up your mind to sort out the ARC-related documents and take out some business time every day and read through it, it's necessary.

ARC for me, just by the general understanding of the use of, on the one hand really no time, on the other hand feel good, to understand and grasp the apple architecture design concept, according to this has been guessing, finally make a joke 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 "adept at calculation" does not have to be too rigorous, rough is not bound.


Discuss
Discussion

Subsequent messages for this object generate an error message that states that the message was sent to a disposed object (which provides 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 to process the resource, not the instance variable that is used to process the object, for example:
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 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, the Dealloc method of any object is called by the runtime. See the Advanced Memory Management Programming Guide for more detailed information.
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. (implicit means that 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.







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.