Mihai Manea
Mihai Manea

Reputation: 11

ios application crashes when taking a screenshot due to memory spike

I have an app with multiple view controllers and navigation controllers. If I switch between two view controllers about 10 times and then take a screenshot from my iphone the application freezes, the memory spikes up to 1 GB and then crushes. If I take the screenshot after 4-5 switching between screens the app does not crash when taking screenshot. I suspect my app is creating multiple instances of the view controllers and I did not find a way to release them. Anyway if you could give me any tip it will much appreciated. In my example I present the two view controllers like this:

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "comanda")
let first = storyBoard.instantiateInitialViewController()
self.present(nextViewController, animated:true, completion:nil)

and

let storyBoard : UIStoryboard = UIStoryboard(name: "Main", bundle:nil)
let nextViewController = storyBoard.instantiateViewController(withIdentifier: "meniuControl4") as! SideMenuController
self.present(nextViewController, animated:true, completion:nil)

Upvotes: 0

Views: 1748

Answers (2)

Mihai Manea
Mihai Manea

Reputation: 11

Thanks, for all the help. I managed to fix the problem using dismiss when going back one step and unwind segues when returning to the main screen. Now the app does not crashes when I take a screenshot no matter how many VCs I go through and come back.

Upvotes: 0

Mr.P
Mr.P

Reputation: 1440

If you're calling the above code from within each of the two view controllers, then you're never dismissing either one, you just keep presenting one on top of another which is adding to memory.

I don't know what the flow of your app is, but if you present a view controller using present(_:animated:completion:) then you should call dismiss(animated:completion:) at some stage from within that view controller to navigate backwards. If you call present(_:animated:completion:) from within it to get to another view controller and keep doing this, then you just keep adding view controllers on top of each other like a deck of cards.

Upvotes: 1

Related Questions