【问题标题】:Swift Decode Array Custom Class Memory LeakSwift 解码数组自定义类内存泄漏
【发布时间】:2016-07-04 00:14:06
【问题描述】:

我正在使用 NSCoding 保存我的应用程序配置,并且在使用 DecodeWithKey 时出现 Instruments 泄漏。

Settings 有一个属性 stsSettings

stsSettings = (aDecoder.decodeObjectForKey("stsSettings") as! StsSettings)

stsSettings 有一个 StsVariables 属性数组

stsVariables = (aDecoder.decodeObjectForKey("stsVariables") as! [StsVariable])

Leaked Object # Address Size 责任库责任框架 StsVariable 1 0x7fe182d494f0 192 字节基础_decodeObjectBinary

Settings 还有一个属性 conversions,它是一个 Conversion 对象数组,它不会泄漏,所以我不知道发生了什么。

【问题讨论】:

    标签: arrays swift memory-leaks nscoding


    【解决方案1】:

    我在类似的情况下遇到了内存泄漏。我通过将解码后的数组分配给局部变量并将元素复制到属性来解决了这个问题。但是,我不知道为什么会出现内存泄漏。

    let variables = (aDecoder.decodeObjectForKey("stsVariables") as! [StsVariable])
    stsVariables = [StsVariable]()
    for variable in variables {
        stsVariables += [variable]
    }
    

    【讨论】:

    • 谢谢 :-) 我基本上做了同样的事情,但解码为 NSArray 并循环播放!不过很奇怪!
    猜你喜欢
    • 2017-08-24
    • 2018-07-15
    • 2011-07-05
    • 1970-01-01
    • 1970-01-01
    • 2020-03-19
    • 2021-11-18
    • 2012-04-21
    • 1970-01-01
    相关资源
    最近更新 更多