Memory is not being relased when I go back and forth between view controllers

71 Views Asked by At

I have an app with a main view controller that has a uitableview that has a user listed in each cell. Each cell has a profile pic. Tapping the profile pic goes to that users profile page using pushViewController() and the memory usage increases like normal. The problem is when I go back to the main page, the memory is still very high (I see so in the debug navigator). If click another profile in the main page the memory increases even more, on and on until the app crashes. Please show me where I am messing up.

My Cell Class looks like:

class LatestCell: UITableViewCell {
weak var delegate: LatestCellDelegator!    
@objc func profPicClick(tapGestureRecognizer: UITapGestureRecognizer){
let tImage = tapGestureRecognizer.view as! UIImageView
if(self.delegate != nil){
            self.delegate.goToProfilePage(userID: fUserId!,  profileImage: tImage )
        }
}    

//I'm keeping my TableView class separate from My main controller class because I use my tableView everywhere. Just trying to be efficient. It looks like this:

`class ReusableTableView:  NSObject, UITableViewDataSource, UITableViewDelegate, LatestCellDelegator {    
     weak var parentCollectionController: UIViewController?
    init(_ tv: UITableView, _ data: [LatestStatus],_ pcc: UIViewController)
    {
        super.init()
        self.tableViewData = data
        self.tableView = tv
        parentCollectionController = pcc
        self.tableView?.register(UINib(nibName: "FreeCell", bundle: nil), forCellReuseIdentifier: "FreeCell")
    }    
    ...
    //Trying to deallocate
    deinit {
        print("deinit is called")
    }    
        ...
        func goToProfilePage(userID dataobjectUID: String, profileImage dataProfileImage: UIImageView) {
        weak var profileVC = storyboard.instantiateViewController(withIdentifier: "RealProfilePage") as? ProfilePage2
        profileVC?.userId = dataobjectUID 
parentCollectionController?.navigationController?.pushViewController(profileVC!, animated: true)
        profileVC = nil
    } 

`

This is the Main controller that holds the tableView. As you see below I have MainViewController as one of the parameters for when I instatiate reusableTableView because that is how I am able to call parentCollectionController?.navigationController?.pushViewController(... in the ReusableTableView class:

class MainViewController: UIViewController {
@IBOutlet weak var theTableview: UITableView!
var usersArray: [Users]?
weak var thisController = self    
override func viewDidLoad() {
reusableTableView = ReusableTableView(theTableview, usersArray!, thisController!)
}

and then finally this is the profile page that is shown when a profile pic is clicked

class ProfilePage2: UIViewController{
//heavy code is doneby using the userId to show pics and network calls etc
  var userId: String?
    deinit {
        print("deinit is being called in Profile Page")
    }  
//deinit is never called by the app for some reason 

Memory usage increases when pushViewController() is called but the memory doesnt reduce when the back button is tapped and I go back to the MainController. Memory increases by 20 MB everytime. How can I release the memory properly. Thanks

0

There are 0 best solutions below