zoukankan      html  css  js  c++  java
  • ARC模式下的内存泄露问题

    ARC模式下的内存泄露问题

    iOS提供的ARC 功能很大程度上简化了编程,让内存管理变得越来越简单,但是ARC并不是说不会发生内存泄露,使用不当照样会发生。

    以下列举两种内存泄露情况:

    死循环造成的内存泄露

    • 若一个ViewController中存在无限循环,就会导致即使ViewController所对应的View消失掉了,ViewController对象也不能够被释放。

      此问题通常发生在animation处理中:

      eg:

      CATransition *transition = [CATransition animation]; transition.duration = 0.5; tansition.repeatCount = HUGE_VALL; [self.view.layer addAnimation:transition forKey:"myAnimation"];

      以上示例中,animation重复设成HUGE_VALL(一个很大的数值),基本上等于无限循环了。

      解决的办法是:当ViewController关掉的时候,停止这个animation。

      eg:

      -(void)viewWillDisappear:(BOOL)animated { [self.view.layer removeAllAnimations]; }

    循环参照造成的内存泄露

    • 甲有一个属性参照为乙,乙有个属性参照为甲,如果甲和乙都为strong参照的话,两个对象都是无法释放的。

      这种问题通常发生于把delegate声明为strong属性了。

      eg:

      @interface ExampleViewController @property (nonatomic, strong) ExampleClass *exampleClass; @end @interface ExampleClass @property (nonatomic, strong) ExampleViewController *delegate; @end

      以上这种问题解决办法只需把ExampleClassdelegate属性的strong改为assign即可。

  • 相关阅读:
    shell:bash基本特性
    python_day02
    python_day01
    centos环境下安装python3以及pip3
    http1到http3的巨大变化
    HTTP协议
    bootstrap
    JQuery
    BOM与DOM
    23种设计模式的几种常见的设计模式
  • 原文地址:https://www.cnblogs.com/YKiOS/p/4976474.html
Copyright © 2011-2022 走看看