gpt4 book ai didi

ios - 使用 Property List/NSMutableDictionary 的内存泄漏

转载 作者:行者123 更新时间:2023-11-28 16:07:54 27 4
gpt4 key购买 nike

我在主包中使用属性列表来存储有关关卡和玩家的信息。第一次启动应用程序时,pList 会被复制到手机上,然后我根据需要从该本地副本访问信息。我在 Instruments 中运行 Leaks,我不断遇到内存泄漏,我认为这些内存泄漏与创建存储在 pList 中的字典和其他数据类型有关。

这是创建字典的地方 - 在找到 pList 的路径之后:

if fileManager.fileExists(atPath: path.path) {

if let dictionaryForPlist = NSMutableDictionary(contentsOf: path) {

return(dictionaryForPlist)
}
else {
print("pList not found")
}

let levelInstanceData = LevelData() //this class searches the main bundle for the plist and stores the pList as an NSMutableDictionary
let currentLevel = levelInstanceData.localDataFile["Level1"] as! Int //localDataFile is the NSMutableDictionary storing the information
let levelName = levelInstanceData.localDataFile["Level1Name"] as! String

我强制将每条数据转换为正确的数据类型,并在整个关卡中使用它。

这是 Instruments 中泄漏对象的屏幕截图。有没有其他人遇到过这个问题或有任何想法如何阻止泄漏?

最佳答案

提供所有泄漏的对象并不过分帮助。您需要做的是查看与每个泄漏对象相关的调用树。这将向您显示泄漏源自代码中的何处。从那里您可以开始推断需要做什么来补救它。

你应该读这个。它已过时,但讨论了调用树。

https://www.raywenderlich.com/2696/instruments-tutorial-for-ios-how-to-debug-memory-leaks

关于ios - 使用 Property List/NSMutableDictionary 的内存泄漏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39969749/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com