ios - 是什么导致了我的 Swift 代码中的这些内存泄漏?
问题描述
有没有人可以告诉我为什么我的代码会出现以下四个内存泄漏(如下所示)以及需要进行的代码更改以纠正它?它是我创建的一个collectionNode,它有一个dataSource 和delegate,并且执行泄漏分析表明了四个泄漏。
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") }
}
解决方案
推荐阅读
- json - Swift JSON 到 Flutter JSON
- ios - 将视图添加到自定义集合视图单元格时出现奇怪的功能
- c# - 公会成员不再填充,不能 GetUserAsync 因为它只返回机器人而没有其他人
- python - 授权标头未确认为所需格式 - Python Cosmos-db api
- python - 如何从模型中获取数据并显示在产品页面上?
- java - 无法将图像上传到 Firebase 存储
- java - 使用协程时出现类型不匹配错误
- rxjs - 如何使 observable 重复
- python - Tkinter 中的条目小部件导致的空白空间
- flutter - 验证谷歌登录的问题