Fast closing with variable memory loss

I am debugging my program with a memory leak problem. In the ViewController, when it pops up, call the deinit function. But this is not the case, so I will try to figure it out in the next step.

After commenting out many lines, I found that the following lines make the ViewController not dealloc.

var api:APIGetList = APIGetList(tripId: trip.id, offsetToken: offsetToken,shouldRemoveAll:self.shouldRemoveAll)
api.request({ (obj, error) -> Void in
    //ingore here
    }, immediateHandler: {[unowned self](obj) -> Void in
        if var tripPosts = obj as? [TripPost]{
            self.tripPosts = tripPosts<--- this line cause the leak
        }
        self.tableView.reloadData()

    }, failHandler: { (errCode, errMsg, error) -> Void in
        self.isLoading = false
        self.refreshControl.endRefreshing()
})

      

in my ViewController, I will call the api to get the data and replace the current list with tripPosts. the line I mentioned "this line is causing a leak", when I comment it, caused a deinit. Therefore, I believe this issue is the cause of the problem.

for the callback object - obj. It comes from the following codes:

var tripPosts = [TripPost]()
tripPosts = TripPost.MR_findAll()
immediateHandler(obj: tripPosts)

      

so what is causing the memory leak?

+3


source to share


2 answers


Try putting [weak self] shortly after api.request ({in



  var api:APIGetList = APIGetList(tripId: trip.id, offsetToken: offsetToken,shouldRemoveAll:self.shouldRemoveAll)

        api.request({ [weak self](obj, error) -> Void in
            //ingore here
            }, immediateHandler: {[weak self](obj) -> Void in
                if var tripPosts = obj as? [TripPost]{
                    self.tripPosts = tripPosts<--- this line cause the leak
                }
                self.tableView.reloadData()

            }, failHandler: {[weak self] (errCode, errMsg, error) -> Void in
                self.isLoading = false
                self.refreshControl.endRefreshing()
        })

      

+4


source


oh ... finally I figured it out. The problem is with the line self.tableView.reloadData () <- and in the cellForTableView I have assigned a delegate to the cells which is not a weak reference ...



so it doesn't involve closure

+2


source







All Articles