我是Leak Instrument的新手,并开始学习如何修复内存泄漏。我有以下代码:
class InputButtonsContainerView: UIView {
var hexButtons = [UIButton]()
required init?(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
//Configure buttons
for view in subviews {
for btn in view.subviews where btn is UIButton {
let button = btn as! UIButton
button.layer.borderWidth = 1
button.layer.borderColor = UIColor.black.cgColor
button.layer.cornerRadius = SizeAdaptation.shared.inputButtonCornerRadius
button.titleLabel?.font = UIFont.systemFont(ofSize: SizeAdaptation.shared.inputButtonFontSize)
if button.tag > 9 && button.tag < 16 {
hexButtons.append(button)
}
}
}
defer{
inputMode = .dec
}
}
var inputMode = MakerInputMode.dec {
didSet{
switch inputMode {
case .dec:
for btn in hexButtons {
btn.layer.borderColor = UIColor.lightGray.cgColor
btn.setTitleColor(UIColor.lightGray, for: .normal)
}
case .hex:
for btn in hexButtons {
btn.layer.borderColor = UIColor.black.cgColor
btn.setTitleColor(UIColor.black, for: .normal)
}
}
}
}
}
在AddColorViewController类中,有一个InputButtonsContainerView的IBOutlet对象:
class AddColorViewController: UIViewController {
@IBOutlet weak var inputButtonsContainerView: InputButtonsContainerView!
....
}
泄漏仪器显示init功能有泄漏:
似乎UIButton
数组被释放两次并导致引用计数为-1。有谁知道如何解决这一问题 ?谢谢。