我经常使用Xcode乐器(8.2)找到“泄漏”,其中free
是对象/内存的最后一次调用。如果成功调用free
那么为什么工具会将此报告为泄漏?这真的是泄漏还是仪器中的错误?
这是一个例子
例如,这一小段代码似乎会导致工具报告这些泄漏。
class RDTranslateComponentView: RDMessageCellComponentView {
let textView: RDTextMessageCellComponentView
let button: UIButton
convenience init() {
self.init(frame: CGRect.zero)
}
override init(frame: CGRect) {
self.textView = RDTextMessageCellComponentView()
self.button = UIButton(type: .custom)
super.init(frame: frame)
self.setupView()
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
private func setupView() {
self.textView.label.font = UIFont.proximaSemiBold(withSize: 14)
self.textView.label.textColor = UIColor.remindBrand()
self.textView.bubble.backgroundColor = UIColor.white
self.addSubviews([ self.textView, self.button ])
self.button.addTarget(self, action: #selector(pressedButton(_:)), for: .touchUpInside)
}
@objc private func pressedButton(_ sender: UIButton?) {
/// Code that calls into a delegate
}
override func sizeThatFits(_ size: CGSize) -> CGSize {
return self.textView.sizeThatFits(size)
}
override func layoutSubviews() {
super.layoutSubviews()
self.textView.frame = self.bounds
self.button.frame = self.bounds
}
}
class RDMessageCellComponentView: UIView {
/// The inside of this is just a bag of constants. Doesn't even touch anything View related so I'm omitting.
}
这是我的代码中与泄漏关联的堆栈跟踪。请记住,这不是唯一发生这种情况的地方。这个案例经常出现并且似乎是在子视图的UILabel
方法中实例化init
的代码所独有的,该子视图被添加为另一个视图的子视图。