iOS dismissing a view controller doesn't release memory

2k Views Asked by At

I know it's all with the weak strong reference when presenting the viewController in the parent view ... correct me please if I'm wrong

this is an example how I do it

let viewHolder = viewClass()

func presentView()  {
    self.present(viewHolder, animated: true) {

    }
}

see my memory monitor only from open and dismiss the same view over and over

enter image description here I have tried this

weak var viewHolder = viewClass()

func presentView()  {
    self.present(viewHolder!, animated: true) {

    }
}

but this would give me a crash

Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value

and dismiss the viewClass this way (inside itself off)

func dismissPage()  {
    self.dismiss(animated: true)
}

any help would be appreciated thanks

2

There are 2 best solutions below

0
Robert D. Mogos On

The problem might not be in the viewClass. You are declaring let viewHolder = viewClass() in your initial class. So when you dismiss the controller, that variable still exists. Do you need to keep a reference to it? If not, you can easily allocate it when needs to be displayed and when you dismiss it, the memory is going to be freed:

func presentView()  {
     // instead of saving viewHolder as an instance variable, you declare it locally
    let viewHolder = viewClass()
    self.present(viewHolder, animated: true) {

    }
}
0
user3350097 On
  1. Use deint method and check weather it is called or not after dismiss your viewcontroller.

  2. if your using any kind of blocks or closure to work done then use [weak self] in side your closure.

  3. your can also make nil your property/delegate/variable when viewDidDisappeared only if after there is no view controller will be presented or pushed