3
메모리 관리 문제가 발생합니다. UIViewController
의 하위 클래스가 있고 참조를 다시 viewController
에두고 참조주기를 피하기 위해 수동으로보기를 설정했습니다. weak/unowned
을 사용합니다. 이제는 문제가 있습니다. unowned
을 사용하면 메모리 누수가 발생하지만 weak
을 사용하면 문제가 발생합니다. 왜 이런 일이 일어날 지 모르겠다.알 수없는 참조가 누수 됨, 약함이 없음
업데이트 : 버그 인 것 같습니다.
콘솔 출력 :
removing vc
view Controller deinitialized
custom view deinitialized
내가 엑스 코드를 8.3.1
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
window = UIWindow(frame: UIScreen.main.bounds)
window?.rootViewController = ViewController(nibName: nil, bundle: nil)
window?.makeKeyAndVisible()
DispatchQueue.main.asyncAfter(deadline: .now() + 5) {
print("removing vc")
self.window?.rootViewController = nil
}
return true
}
class ViewController: UIViewController {
override func loadView() {
view = CustomView(frame: .zero, vc: self)
view.backgroundColor = .red
}
deinit {
print("view Controller deinitialized")
}
}
class CustomView:UIView{
init(frame: CGRect , vc:ViewController) {
self.vc = vc
super.init(frame: frame)
}
required init?(coder aDecoder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
// weak var vc : ViewController! // no leak
unowned var vc : ViewController // leak
deinit {
print("custom view deinitialized")
}
}
누출 여부는 어떻게 결정합니까? 어쩌면 당신의 방법이 잘못되었거나 컴파일러 버그 일 가능성이 낮습니다. – Alistra
악기와 메모리 그래프 모두 이것이 누출임을 보여줍니다. – Alan
약하거나 소유하지 않은 사람은 누구도 참고할 수 없습니다. 그래서 누출의 이유가 될 수 없습니다. 참조 : http://stackoverflow.com/a/26025176/6595536 – ObjectAlchemist