zoukankan      html  css  js  c++  java
  • iOS 在 ARC 环境下 dealloc 的使用、理解误区

    iOS 在 ARC 环境下 dealloc 的使用、理解误区

    太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es)

    本文遵循“署名-非商业用途-保持一致”创作公用协议

    转载请保留此句:太阳火神的漂亮人生 -  本博客专注于 敏捷开发及移动和物联设备研究:iOS、Android、Html5、Arduino、pcDuino否则,出自本博客的文章拒绝转载或再转载。谢谢合作。


    下图最后一句话。解开了俺接触 ARC 以来一直的误解:

    在 ARC 环境下,重载的 dealloc 方法一样会被调用。仅仅只是,不能在该方法的实现中调用父类的该方法。

    以下看个演示样例来验证一下:

    一个待測试的类 Test,创建和销毁它,在 ARC 环境下看看 dealloc 是否被调用了;第二就是在 dealloc 中调用父类的实现,看看会如何。

    还有一个是视图控制器,用于加入两个button,当中一个button的事件方法用于创建 Test 类。还有一个用于释放,这里相同測试了一个 ARC 中当一个对象被 0 个引用指向时。是否立即释放。


    Test 类的声明例如以下:

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


    Test 类的声明和实现例如以下:

    #import "Test.h"
    
    @implementation Test
    
    
    - (id)init {
        
        
        self = [super init];
        if (self) {
            
            
        }
        return self;
    }
    
    - (void)dealloc {
        
        NSLog(@"dealloc");
        //[super dealloc];
    }
    
    @end
    


    视图控制器声明:
    #import <UIKit/UIKit.h>
    #import "Test.h"
    
    
    @interface ViewController : UIViewController
    
    @property (nonatomic, strong) Test *test;
    
    @end
    



    视图控制器实现:
    #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
    

    经測试。一切正常,并且在按释放button时,Test 类的对象的 dealloc 方法会马上被调用。基本没有感觉到延迟。

    另外,当在 Test 类的 dealloc 方法最后调用父类的 dealloc 方法实现时,XCode 5.1.1 会自己主动出现提示。例如以下图:

    ARC 拒绝 显式 ‘dealloc’ 消息发送。



    至此。下定决心,把 ARC 相关的文档整理,并每天抽出点业务时间,好好通读一遍,这个非常有必要。

    ARC 对我来说。仅仅是凭大概理解就用起来了。一方面确实没时间,还有一方面自我感觉良好,以为理解和掌握了苹果的架构设计理念。按此一直往下猜。最终闹出今天的笑话来。

    凡事,还是要从官方的资料中求证一下才好,也免于每次用时的为不确定而分心。


    “精于计算”首先须要严谨;而“精于算计”就不用太过严谨。粗犷才不至于束缚。


    讨论
    Discussion

    该对象的兴许消息会生成一个错误信息。该信息指出消息发送给了一个已释放的对象(提供了未被重用的已释放内存)。


    Subsequent messages to the receiver may generate an error indicating that a message was sent to a deallocated object (provided the deallocated memory hasn’t been reused yet).

    你重载该方法用于处理资源。而不是用于处理对象的实例变量。比如:
    You override this method to dispose of resources other than the object’s instance variables, for example:

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


    在 dealloc 的实现中,不要调用父类的实现。

    你应该尽量避勉使用 dealloc 管理有限资源诸如文件描写叙述符的生命周期。


    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.

    决不要直接发送 dealloc 消息。

    相反,不论什么对象的 dealloc 方法由执行时调用。參看  高级内存管理编程指南 以获得更具体的内容。
    You never send a dealloc message directly. Instead, an object’sdealloc method is invoked by the runtime. See Advanced Memory Management Programming Guide for more details.

    特别注意事项
    Special Considerations

    当未使用 ARC 时,你的 dealloc 实现必须把调用父类的实现作为最后一条指令。(隐含的意思就是。使用 ARC 时不能调用父类的实现)
    When not using ARC, your implementation of dealloc must invoke the superclass’s implementation as its last instruction.







  • 相关阅读:
    Python操作MySQL数据库
    Spark学习--Structured Streaming
    YumRepo Error: All mirror URLs are not using ftp, http[s] or file.
    Spark学习--SparkStreaming
    Spark学习--SparkSQL04
    Spark学习--SparkSQL03
    Spark学习--SparkSQL02
    Bootstrap简介
    Linux Shell系列教程之(一)Shell简介
    Linux命令之必须掌握的十条命令
  • 原文地址:https://www.cnblogs.com/yxysuanfa/p/7093795.html
Copyright © 2011-2022 走看看