Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Storyboard segues causing memory leaks

I have two UIViewControllers with buttons triggering segue (modal) to each other. I wanted to discover if that's causing any memory leaks while jumping back and forth and I see that Living Object && allocated memory is going up, what eventually would leave to app crash. I don't have any single line of code - working with pure UIViewControllers. storyboard

profiler 1

profiler 2project's settings

What I might be doing wrong?
Could have I set something wrong in project settings?
Am I reading profiler's statictics badly?
Do I need to make any special release commands while working with segues?

like image 929
Piotr Avatar asked Oct 28 '12 03:10

Piotr


People also ask

What could be the possible cause of memory leaks?

In computer science, a memory leak is a type of resource leak that occurs when a computer program incorrectly manages memory allocations in a way that memory which is no longer needed is not released. A memory leak may also happen when an object is stored in memory but cannot be accessed by the running code.

What causes memory leaks Swift?

Memory leaks in Swift are often the product of a retain cycle, when one object will hold a strong reference to an object that also strongly references the original object. So A retains B and B retains A . These kinds of issues can sometimes be tricky to debug and lead to hard to reproduce crashes.

Can a memory leak cause stack overflow?

"Memory leak", in brief, refers to the scenario that memory is allocated but not released even if it is no longer needed. A stack overflow itself is NOT causing any unneeded memory to fail to be released. There is no reason that you can treat a stack overflow as a "memory leak".


1 Answers

You aren't using the modal segues correctly. The way you have implemented it, you are creating a new instance of each view controller when you segue instead of returning to the instance you came from. That is why your memory usage continues to increase.

Before iOS 6, the correct way to handle this was to:

1) define a method such as viewController2Done in view controller 1
2) in view controller 2, create a property called delegate of type id.
3) in prepareToSegue for view controller 1, set delegate in view controller 2 to self
4) in view controller 2, when it is time to return to view controller 1, call [delegate viewController2Done]
5) in viewController2Done call [self dismissModalViewControllerAnimated:YES]

This method still works in iOS 6, but there is also a new unwind segue that can be used instead. To use it, you would define a method in your view controller 1 like so:

Objective-C:

- (IBAction)unwindFromViewController2:(UIStoryboardSegue *)segue
{
    NSLog(@"and we are back");
}

Swift:

@IBAction func unwindFromViewController2(_ segue: UIStoryboardSegue) {
    print("and we are back")
}

Then you'd control drag from the button in view controller 2 to the orange exit icon in the bar above the view controller in the Storyboard. In the pop up, you'd select unwindFromViewController2 and voila, you're done.

enter image description here

like image 106
vacawama Avatar answered Sep 28 '22 11:09

vacawama