gbusto / BrickBreak

An attempt to make a game like "Ballz" or "Bricks and Balls".
1 stars 0 forks source link

Returning to Game Menu from a Game Mode Doesn't Remove the View Controller from Memory #607

Closed gbusto closed 5 years ago

gbusto commented 5 years ago

This is a pretty big bug and probably has an impact on performance. I found this by printing out statements when the app goes into the background and returns to the foreground. What I noticed is that it works as expected in classic or levels mode and I see the notification print out. But when I return to game menu and select a new game mode, I see 2 notifications each time because the previous view controller is still responding to those same notifications. I'm not sure what needs to be done to fix this but it needs to be fixed ASAP.

gbusto commented 5 years ago

I don't actually think this was a bug. I think the view controller may actually remain in memory to speed up reloading it and it still responds to notification events in the background. I fixed this issue by calling removeObserver on the background/foreground/terminate notifications.

gbusto commented 5 years ago

I think the actual reason for this is that the game makes the reference to my view controllers a strong reference because they're designed in the storyboard and Swift knows I might reuse it again. If the ViewController was instantiated programmatically (weak reference meaning nothing else explicitly asked to hold a reference to it) maybe it would actually deallocate memory. I discovered this because of the deinit function. That function supposedly runs when the ViewController deallocates and will be removed from memory. But in my ViewControllers that doesn't ever get called, and the answer I found said that something must be holding a strong reference to my ViewControllers. References are strong by default. So that explains the behavior I was seeing here.

gbusto commented 5 years ago

https://krakendev.io/blog/weak-and-unowned-references-in-swift Link explaining strong vs weak references.