Skip to content
This repository was archived by the owner on Aug 8, 2023. It is now read-only.
This repository was archived by the owner on Aug 8, 2023. It is now read-only.

[IOS] Memory leak #7991

Closed
Closed

Description

@karlobutorac

Platform: ios
**Mapbox SDK version:**3.4.1

Steps to trigger behavior

  1. 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 :)

Activity

  1. 1ec5 commented on Feb 8, 2017

    @1ec5
    Contributor

    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!

  2. added
    iOS
    performance
    on Feb 8, 2017
  3. karlobutorac commented on Feb 9, 2017

    @karlobutorac
    Author

    So this is the code https://paste.ofcode.org/CGYNffQ8jS8F55cHCi4TZR
    and this is how the storyBoard looks like ,just 2 buttons to switch viewControllers
    screen shot 2017-02-09 at 12 55 09 am
    and this is how the memory looks (after a few more spikes the app crashes)
    screen shot 2017-02-09 at 12 59 20 am

  4. 1ec5 commented on Feb 9, 2017

    @1ec5
    Contributor

    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 View Debugger. Notice how deep the stack of UITransitionViews is:

    UITransitionView stack

    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!

  5. karlobutorac commented on Feb 9, 2017

    @karlobutorac
    Author

    It works as a charm! Thank you very much :D

  6. rajtharan-g commented on Apr 24, 2018

    @rajtharan-g

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    iOSperformance

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions