Misunderstanding of using and understanding dealloc in the ARC environment of iOS

Source: Internet
Author: User

Misunderstanding of using and understanding dealloc in the ARC environment of iOS

Beautiful Life of the sun and fire god (http://blog.csdn.net/opengl_es)

This article follows the "signature-non-commercial use-consistency" creation public agreement

Reprinted please keep this sentence: Sun huoshen's beautiful life-this blog focuses on Agile development and mobile and IOT device research: iOS, Android, Html5, Arduino, pcDuino, otherwise, this blog post is rejected or reprinted. Thank you for your cooperation.


In the last sentence, the misunderstanding that I have been in contact with ARC has been solved:

In the ARC environment, the overloaded dealloc method will be called, except that the method of the parent class cannot be called in the implementation of this method.

The following is an example for verification:

Test a class to be tested. create and destroy it. In the ARC environment, check whether dealloc is called. The second is to call the implementation of the parent class in dealloc to see what will happen.

The other is the view controller, which is used to add two buttons. The event Method of one button is used to create the Test class, and the other is used to release, it also tests whether an object in an ARC is immediately released when it is pointed to by 0 references.


The Declaration of the Test class is as follows:

#import 
 
  @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 self;}- (void)dealloc {        NSLog(@"dealloc");    //[super dealloc];}@end


View Controller declaration:
#import 
 
  #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 that can be recreated.}- (IBAction)generateButtonPressed:(UIButton *)sender {        self.test = [[Test alloc] init];}- (IBAction)removeButtonPressed:(UIButton *)sender {        self.test = nil;}@end

After testing, everything is normal, and when you press the release button, the dealloc method of the Test class object will be called immediately, basically without any delay.

In addition, when the dealloc method of the Test class finally calls the dealloc method of the parent class, XCode 5.1.1 will automatically prompt, such:

ARC rejects explicit 'delealloc 'message sending.



So far, we have made up our mind to sort out ARC-related documents and take some business time every day to read them all through. This is necessary.

For me, ARC is only used based on a rough understanding. On the one hand, it does not have time, and on the other hand, it feels good to myself and thinks that it has understood and mastered Apple's architectural design philosophy, continue to guess, and finally make a joke today.

In everything, we still need to verify it from official documents, and avoid being distracted by uncertainty every time.


"Excellent computing" requires rigorous first, while "precise computing" does not require too rigorous, so it is not difficult to be rough.


Discussion
Discussion

An error message is generated for subsequent messages of this object, indicating that the message is sent to a released object (which provides unused released memory ).
Subsequent messages to the caller may generate an error indicating that a message was sent to a deallocated object (provided the deallocated memory hasn't been reused yet ).

You can use this method to process resources, rather than instance variables of objects. For example:
You override this method to dispose of resources other than the object's instance variables, for example:

- (void)dealloc {    free(myBigBlockOfMemory);}


In dealloc implementation, do not call the implementation of the parent class. You should avoid using dealloc to manage the lifecycle of limited resources such as file descriptors.
In an 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 directly send dealloc messages. On the contrary, the dealloc method of any object is called at runtime. See the advanced memory management programming guide for more details.
You never send a dealloc message directly. Instead, an object 'sdealloc method is invoked by the runtime. SeeAdvanced Memory Management Programming Guide for more details.

Special notes
Special Considerations

When ARC is not used, your dealloc implementation must call the implementation of the parent class as the last instruction. (The implicit meaning is that the implementation of the parent class cannot be called when ARC is used)
When not using ARC, your implementation of dealloc must invoke the superclass's implementation as its last instruction.







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.