简体   繁体   English

Xcode NSKeyedUnarchiver内存泄漏SKNode扩展

[英]Xcode NSKeyedUnarchiver Memory Leak SKNode Extension

After days and days of researching and trying my best, I have not found a solution to a memory leak problem. 经过数天的研究和尽力而为,我还没有找到解决内存泄漏问题的方法。 I have already tried very many ideas I found in this forum but nothing seemed to help me out. 我已经尝试过在这个论坛中找到的很多想法,但是似乎没有任何帮助。

I load my GameScene.sks with this code like everyone else does: 我像其他所有人一样,用以下代码加载我的GameScene.sks:

extension SKNode {

class func unarchiveFromFile(file:String) -> SKNode? {
    if let path = Bundle.main.path(forResource: file, ofType: "sks") {
        let url = URL(fileURLWithPath: path)
        do {
            let sceneData = try Data(contentsOf: url, options: .mappedIfSafe)
            let archiver = NSKeyedUnarchiver(forReadingWith: sceneData)
            archiver.setClass(self.classForKeyedUnarchiver(), forClassName: "SKScene")
            let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode
            archiver.finishDecoding()
            return scene
        } catch {
            print(error.localizedDescription)
            return nil
        }
    } else {
        return nil
    }
}

and i am using it in my code here: 我在这里的代码中使用它:

func load(level: String) {
    if let levelNode = SKNode.unarchiveFromFile(file: level) {
        mapNode = levelNode
        mapNode.name = "MapNode"
        self.worldLayer.addChild(mapNode)
        loadTileMap()
    }
}

If I leave out the line self.worldLayer.addChild(mapNode) everything works perfectly and the memory usage will never rise. 如果我忽略了self.worldLayer.addChild(mapNode)行, self.worldLayer.addChild(mapNode)一切将正常运行,并且内存使用量将永远不会增加。 But if I use it (I need it!), memory keeps climbing and climbing. 但是,如果我使用它(我需要它!),内存将不断攀升。

With use of Instruments it says that the line with let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode 通过使用Instruments,它说的是let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode causes all the memory leaks. let scene = archiver.decodeObject(forKey: NSKeyedArchiveRootObjectKey) as! SKNode导致所有内存泄漏。

Unfortunately I do not get it and have not managed to remove my leaks. 不幸的是我没有得到,也没有设法消除泄漏。

I appreciate all help. 我感谢所有帮助。 Thanks in advance. 提前致谢。

Well, I still couldn´t fix the issue but I found out some things. 好吧,我仍然无法解决此问题,但我发现了一些问题。 I can put everything I want in the scene editor (.sks) file and I will not get any memory leaks. 我可以将所需的所有内容放入场景编辑器(.sks)文件中,而不会发生任何内存泄漏。 Even Tile Maps. 甚至平铺地图。 But as soon as I put only one tile in some tilemap memory leaks are rising very high again. 但是,一旦我在某些tilemap中仅放置了一个tile,内存泄漏就会再次上升。 How can one single tile cause all those issues? 一个瓷砖如何引起所有这些问题?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM