zoukankan      html  css  js  c++  java
  • iOS ARC下内存管理使用非自己生成对象的相关问题

    一、用于测试的自定义类
    @interface CustomedObj:NSObject
    
    + (instancetype) methodInit;
    @end
    
    @implementation CustomedObj
    
    + (instancetype)methodInit {
        CustomedObj *temp = [[CustomedObj alloc] init];
        return temp;
    }
    
    - (void) print {
        NSLog(@"==== Customed Object print");
    }
    
    - (void)dealloc {
        NSLog(@"对象即将被析构");
    }
    @end
     
    二、实验结论
          2.1  非自己生成的对象,即alloc/new/copy/mutableCopy以外的创建方法取得对象,并且赋值给非strong变量时,对象将会自动注册至autoreleasepool,从而不至于立刻被析构; (3.1)
          2.2  当赋值给给strong变量时,编译器作自动优化插入objc_retainAutoreleaseedReturnValue, 而不是插入retain,以避免注册至autoreleasePool.(3.3)
          2.3  访问weak变量时,将会把指向对象注册至autoreleasePool.(3.3)。另外,对weak变量再访问会再注册,访问几次则会注册几次,读者可自行验证。
     
    三、实验代码
          3.1   非alloc/new/copy/mutableCopy获取对象,自动注册至autoreleasepool
            __weak CustomedObj *weakRef = [CustomedObj methodInit];// 对象自动注册至autoreleasepool,执行完retainCount=1
            [weakRef print];//执行完retainCount=1
        }
        NSLog(@"退出 autoreleasepool”);
    
    Log:
     ==== Customed Object print
     对象即将被析构
     退出 autoreleasepool

          3.2   alloc/new/copy/mutableCopy获取对象,不会自动注册至autoreleasepool,必须主动强持有以保证不被析构

            __weak CustomedObj *weakRef = [[CustomedObj alloc] init];//编译警告,对象析构
            [weakRef print]; // print消息将被发至nil
        }
        NSLog(@"退出 autoreleasepool”);
    
    LOG:
     对象即将被析构
     退出 autoreleasepool

          3.3   weak指针被访问时,如未注册,系统自动将对象注册至autoreleasepool,引用计数增加

            CustomedObj *strongRef = [CustomedObj methodInit];//变量默认为__strong,因为编译器优化,将不会注册至autoreleasepool, retainCount=1
            __weak CustomedObj *weakRef = strongRef;
            [weakRef print];//调用weak指针会将对象注册到autoreleasepool,执行完retainCount=2
        }
        NSLog(@"退出 autoreleasepool");
    LOG:
     ==== Customed Object print
     对象即将被析构
     退出 autoreleasepool
  • 相关阅读:
    java基础
    C++菜鸟启动之旅--vc6.0使用教程详解
    第8章 Linux磁盘与文件系统管理
    IO(四)----对象的序列化
    IO(三)----序列流
    IO(二)----字符流
    IO(一)----字节流
    File类
    枚举类
    自动装箱和自动拆箱
  • 原文地址:https://www.cnblogs.com/hushuai-ios/p/5165458.html
Copyright © 2011-2022 走看看