在我的Swift代码中导致这些内存泄漏的原因是什么?

时间:2018-07-13 21:17:04

标签: ios swift memory-leaks sprite-kit

有人可以告诉我为什么我的代码出现以下四个内存泄漏(如下所示)以及需要进行代码更改以纠正它的原因吗?这是我创建的collectionNode,它具有一个dataSource和委托,执行泄漏分析可指示这四个泄漏。

enter image description here

weak var dataSource: CollectionNodeDataSource? { didSet{ setupData() } }

public func setupData() {

    let numberOfItems = dataSource!.numberOfItems() //LEAK 4bytes

    var j: Int = 0

    for i in numberOfCells/2..<numberOfCells {

        let item = dataSource!.collectionNode(self, itemFor: i) //LEAK 4bytes
        item.index = i
        item.id = j
        array_cells.append(item)
        j = j == numberOfItems-1 ? 0 : j+1

    }

    j = numberOfItems-1

    if array_cells[0].nameLabel != nil { cellHeight = array_cells[0].nameLabel!.frame.size.height } //LEAK 25bytes

    for i in (0...(numberOfCells/2)-1).reversed() {

        let item = dataSource!.collectionNode(self, itemFor: i)
        item.index = i
        item.id = j

        array_cells.insert(item, at: 0)
        j = j == 0 ? numberOfItems-1 : j-1

        cumultive_posY = cumultive_posY + cellHeight + spaceBetweenItems

    }

    for item in array_cells { itemNode?.addChild(item) } //LEAK 4bytes

    if currentIndex == -1 {

        currentIndex = numberOfCells/2

    } else {

        if let id = array_cells[currentIndex].id { currentIndex = (numberOfCells/2)+id }

    }

    biggestItem = array_cells.sorted{ $0.calculateAccumulatedFrame().size.height > $1.calculateAccumulatedFrame().size.height }.first! //LEAK 23b
    setSpacing()

    DispatchQueue.main.async { [weak self] in //LEAK 1 byte

        self?.snap(to: self!.currentIndex, withDuration: 0)
        self?.updateItemAppearance(aboutIndex: self!.currentIndex)
        self?.itemNode?.isHidden = false

    }

}

protocol CollectionNodeDataSource: class {

    func numberOfItems() -> Int
    func collectionNode(_ collection: CollectionNode, itemFor index: Index) -> CollectionNodeItem

}

数据模型如下:

public class CapOptionsModel {

    static var `default`: CapOptionsModel = CapOptionsModel()

    let array_options = [ApetureCapSpinnerOption(mode: .liveStream, name: "Stream", image: #imageLiteral(resourceName: "ButtonTip_LiveStream")),
    ApetureCapSpinnerOption(mode: .sessionCapture, name: "Capture", image:#imageLiteral(resourceName: "ButtonTip_SessionCapture"))]

    private init() {

    }

}

CollectionNodeItem就像这样:

class CollectionNodeItem: SKNode {

    fileprivate weak var collection: CollectionNode? { return self.parent as? CollectionNode }
    weak var nameLabel: SKLabelNode?
    weak var imageNode: SKSpriteNode?

    var id : Int!
    var index : Index!

    public override init() {

        super.init()
        isUserInteractionEnabled = true

        let nameLabel = SKLabelNode(fontNamed: FONT_EUROEXT)
        addChild(nameLabel)
        self.nameLabel = nameLabel

        let imageNode = SKSpriteNode()
        addChild(imageNode)
        self.imageNode = imageNode

    }

    required public init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been implemented") }


}

0 个答案:

没有答案