You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Repository navigation
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
Switching between 2 viewControllers in which one is a MGLMapView takes every time about 50-70 mb , resulting in a crash after 6-7 takes.
I found the same issue here: #3130, but it says it's resolved by an update...
I don't know if it matters but I'm using mapView for offline maps.
It is my first time posting anything to github and I'm quite new to swift (and programming) so please if you could explain the solution as best as you can :)
Please link to an Instruments trace and/or sample code that reproduces the problem, so we can be sure we’re looking at the same thing as you are. Thank you!
Based on the storyboard screenshot above, it looks like you’re attempting to implement a Back button by connecting (Control-dragging) a segue from that button to the view controller to the left. Note that a segue always creates a new instance of the view controller and pushes it onto the stack. So what you’re seeing is that, each time you press a Button button, your application creates a brand-new view controller containing an MGLMapView, instead of alternating back and forth between just two view controllers. (The fact that one view controller sits to the right of the other in the storyboard doesn’t matter at runtime.)
You can see this happening at runtime by opening the View Debugger. Notice how deep the stack of UITransitionViews is:
If you want the Back button to go back to an existing instance of a view controller – popping the current view controller off the stack – you’d want to create an unwind segue instead. However, depending on your application’s design, you may find it more convenient to embed the two view controllers in a UINavigationController. The navigation controller manages a Back button for you, automatically labeling it with the title of the previous view controller. This way your application can look and behave in line with user expectations.
One way to distinguish a Mapbox-specific problem from a more general programming problem appropriate for Stack Overflow is to try making a reduced project (as you did), then remove the Mapbox-specific code to see if the problem persists. In this case, if you remove the MGLMapView from the view controller, I think you’d find that the memory would still inch upward as you press the buttons. (The rate of increase would be much smaller, because MGLMapView is a pretty heavyweight object.)
Please let us know if that solves your problem. For more detailed guidance as you learn iOS and Swift development, consider searching and posting to Stack Overflow. Good luck!
The main reason for this memory leak is that the view controller might have not been deallocated.
You can check whether the view controller where you are using the map box map is properly getting deallocated.
You can use the below code in a view controller to know if that has been deallocated when you dismiss/pop a view controller or change a root view controller where you have the map.
deinit {
print("Memory to be released soon")
}
Activity
1ec5 commentedon Feb 8, 2017
Please link to an Instruments trace and/or sample code that reproduces the problem, so we can be sure we’re looking at the same thing as you are. Thank you!
karlobutorac commentedon Feb 9, 2017
So this is the code https://paste.ofcode.org/CGYNffQ8jS8F55cHCi4TZR


and this is how the storyBoard looks like ,just 2 buttons to switch viewControllers
and this is how the memory looks (after a few more spikes the app crashes)
1ec5 commentedon Feb 9, 2017
Based on the storyboard screenshot above, it looks like you’re attempting to implement a Back button by connecting (Control-dragging) a segue from that button to the view controller to the left. Note that a segue always creates a new instance of the view controller and pushes it onto the stack. So what you’re seeing is that, each time you press a Button button, your application creates a brand-new view controller containing an MGLMapView, instead of alternating back and forth between just two view controllers. (The fact that one view controller sits to the right of the other in the storyboard doesn’t matter at runtime.)
You can see this happening at runtime by opening the
View Debugger. Notice how deep the stack of UITransitionViews is:
If you want the Back button to go back to an existing instance of a view controller – popping the current view controller off the stack – you’d want to create an unwind segue instead. However, depending on your application’s design, you may find it more convenient to embed the two view controllers in a UINavigationController. The navigation controller manages a Back button for you, automatically labeling it with the title of the previous view controller. This way your application can look and behave in line with user expectations.
One way to distinguish a Mapbox-specific problem from a more general programming problem appropriate for Stack Overflow is to try making a reduced project (as you did), then remove the Mapbox-specific code to see if the problem persists. In this case, if you remove the MGLMapView from the view controller, I think you’d find that the memory would still inch upward as you press the buttons. (The rate of increase would be much smaller, because MGLMapView is a pretty heavyweight object.)
Please let us know if that solves your problem. For more detailed guidance as you learn iOS and Swift development, consider searching and posting to Stack Overflow. Good luck!
karlobutorac commentedon Feb 9, 2017
It works as a charm! Thank you very much :D
rajtharan-g commentedon Apr 24, 2018
The main reason for this memory leak is that the view controller might have not been deallocated.
You can check whether the view controller where you are using the map box map is properly getting deallocated.
You can use the below code in a view controller to know if that has been deallocated when you dismiss/pop a view controller or change a root view controller where you have the map.
deinit {
print("Memory to be released soon")
}
SOLUTION:
If the above code is not getting called, check for any delegate for which there might be a strong reference. You can fixit by changing that to a weak reference. Checkout "Bonus: View Controller - weak delegate" section in https://samwize.com/2016/05/30/finding-retain-cycle-with-instruments/ for more information.