美文网首页swiftSwift
Swift - Memory Safety

Swift - Memory Safety

作者: ienos | 来源:发表于2018-10-31 15:35 被阅读29次

    默认,Swift 会阻止不安全行为

    不安全行为
    • 确保变量在使用之前被初始化
    • 内存在释放之后不能访问
    • 数据越界报错
    Swift 做了什么?
    • Swift 要求代码独立访问修改该内存,确保多次访问相同内存不会冲突
    • Swift 自动管理内存,大多数情况下不必考虑内存访问
    我们该做什么?
    • 尽管如此,还是需要知道潜在冲突发生,避免代码造成内存访问冲突
    • 当访问内存出现冲突时,将会获得运行时或者编译错误
    访问冲突的简单例子

    购物时新增购物项目的两个步骤

    • 添加购物项
    • 改变金额总数

    内存中变化状态

    • Before: 未新增购物项,未改变金额总数
    • During: 新增购物项,未改变金额总数
    • After: 新增购物项,改变金额总数

    在 During 中总购物项目发生改变,但是金额总数却未发生改变,将会读取到错误信息。我们可能会根据多种方式去修复冲突产生不同结果,根据实际情况获取我们所要的正确金额(有可能是 Before Account,也有可能是 After Account)

    下面讨论的都是单线程问题,并发和多线程冲突访问也许会出现相同问题

    内存访问特性

    两个访问发生冲突的所有条件
    • 至少有一个是可写访问
    • 他们访问同一块内存
    • 他们访问的持续时间重叠
    instantaneous & long - term
    • 短暂访问:同一时间内,不可能被其他代码访问
    • 两个短暂访问不会在同一时间发生
    • 大多数内存访问是短暂的
    • 一个持续访问会和另外一个持续访问发生重叠
    Conflicting Access - In-Out 参数
    • 输入输出参数 inout 是持续访问的,持续到方法调用的整个期间
    var stepSize = 1
    func increment(_ number: inout Int) { // write number 
        number += stepSize // read stepSize
    }
    
    increment(&stepSize) // Error: conflicting accesses to stepSize
    print("\(stepSize)")
    

    解决方法

    var copyOfStepSize = stepSize // copy
    increment(&copyOfStepSize)
    stepSize = copyOfStepSIze
    
    同一个变量传多个 inout 参数
    func balance(_ x: inout Int, _ y: inout Int) { 
        let sum = x + y
        x = sum / 2
        y = sum - x
    }
    
    var playerOneScore = 42
    var playerTwoScore = 30
    balance(&playerOneScore, &playerTwoScore) // Success
    balance(&playerOneScore, &playerOneScore) // Error
    

    Conflicting Access - SELF

    struct Player {    
        var name: String
        var health: Int
        var energy: Int
      
        static let maxHealth = 10
        mutating func restoreHealth() {  
            health = Player.maxHealth
        }
    }
    
    extension Player {
        mutating func shareHealth(with teammate: inout Player) { // write teammate
            balance(&teammate.health, &health) // write self 
        }
    }
    
    var oscar = Player(name: "Oscar", health: 10, energy: 10)
    var maria = Player(name: "Maria", health: 5, energy: 10)
    oscar.shareHealth(with: &maria) // OK
    oscar.shareHealth(with: &oscar) // Error
    

    Conflicting Access - Property

    • Structures / Tuples / Enumerations 由单独的值组成,例如结构体的属性或元祖的元素
    • 以上类型都是值类型,对值的任何部分修改,整个值都会改变
    • 意味着读写访问其中一个属性,都需要读写整个值

    一个写的操作到元祖元素需要写入整个元祖。意味着他们有两个访问到 playerInformation 在重叠的区间,造成冲突

    var playerInformation = (health: 10, energy: 20)
    balance(&playerInformation.health, &playerInformation.energy)
    // Error: Conflicting access property of playerInformation 
    
    当不是全局变量而是局部变量时是安全的
    func someFunction() {
         var oscar = Player(name: "Oscar", health: 10, energy: 10)
         balance(&oscar.health, &oscar.energy) // OK
    }
    

    👇几种情况访问结构体的属性是安全的

    • 只访问实例的存储属性,而不是计算属性和类属性
    • 结构体是一个局部变量的值,而不是全局变量
    • 结构体不是被任何闭包捕获 OR 仅被非逃逸闭包捕获

    相关文章

      网友评论

        本文标题:Swift - Memory Safety

        本文链接:https://www.haomeiwen.com/subject/udbotqtx.html