我有一个非常奇怪的错误,我没有在objective-c。
我有两个导航控制器一个接一个。首先,我有UITableView。在单元格上单击我导航到第二个控制器,然后单击后退按钮我导航到第一个导航控制器。 (我什么都不做。)每次我导航到第二个控制器时我的记忆都会上升,但是当我回去时它不会下降。
我的代码:
First View Controller:
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
performSegue(withIdentifier: "segue", sender: self)
}
// MARK: - Navigation
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "segue",
let destination = segue.destination as? SecondViewController,
let rowIndex = table.indexPathForSelectedRow {
let item = allItems[rowIndex.section][rowIndex.row]
destination.itemId = segue.id as Int?
destination.coreDataManager = self.coreDataManager
}
}
第二视图控制器
override func viewDidLoad() {
super.viewDidLoad()
// reload data
reloadData()
}
private func reloadData() {
// We need database ready to reload data from server
guard coreDataManager != nil else {
print("We try to prepare view, but core data is not ready jet.")
// stop loader
self.loader.stopLoader()
return
}
self.model = Model.init(itemId: itemId! as NSNumber!, managadContext: coreDataManager!.privateChildManagedObjectContext())
}
Model
对象是Objective-c库中的对象。
我知道这个对象有问题,因为如果我注释掉最后一行,步骤记忆图就会消失。
我在之前的Objective-C应用程序中使用相同的调用同一个库,我没有这个内存泄漏。
我尝试:
deinit {
self.model = nil
}
但它没有帮助。
如何解决这个内存泄漏问题,因为如果你看一下图表就会非常庞大。打开4个单元后,我使用了187 MB的内存。
我认为永远不会调用deinit {}
。
SUGGESTION
我将coreDataManager设为弱var:
weak var coreDataManager: CoreDataManager? // weak property of Second Controller
第二个控制器
import UIKit
import BIModel
class SecondViewController: UIViewController {
// MARK: - Properties
/// This object represent connection to database.
weak var coreDataManager: CoreDataManager?
/// This is a Server object that represent on witch server we try to connect.
var currentServer: Server?
/// This is a cube id that we would like to open in this dashboard.
var itemId: Int? = 1193 // TODO: replace this
/// This is a model object. This object holds all calculations, all data,...
var model : Model?
// MARK: - Life Cicle
override func viewDidLoad() {
super.viewDidLoad()
// reload data
reloadData()
}
private func reloadData() {
// We need database ready to reload data from server
guard coreDataManager != nil else {
print("We try to prepare view, but core data is not ready jet.")
return
}
guard itemId != nil else {
return
}
self.model = Model.init(cubeId: currentCubeId! as NSNumber!, managadContext: coreDataManager!.privateChildManagedObjectContext())
}
einit {
print("DEINIT HAPPENED")
model = nil
}
}
我清理了一点代码。这是现在的整个代码。打印出“DEINIT HAPPENED”,但内存堆栈是相同的。