2

I have a method which I use to se tup and show annotation on the map:

func setupPlacemark(place: Place, mapView: MKMapView) {

    guard let location = place.location else { return }

    let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(location) { [unowned self] (placemarks, error) in

        if let error = error {
            print(error)
            return
        }

        guard let placemarks = placemarks else { return }

        let placemark = placemarks.first

        let annotation = MKPointAnnotation()
        annotation.title = place.name
        annotation.subtitle = place.type

        guard let placemarkLocation = placemark?.location else { return }

        annotation.coordinate = placemarkLocation.coordinate
        self.placeCoordinate = placemarkLocation.coordinate

        mapView.showAnnotations([annotation], animated: true)
        mapView.selectAnnotation(annotation, animated: true)
    }
}

When I run Leaks tool I see this leak:

image

And here you will see which line of code doesn’t like:

image

What I should do to make this leak go away?

Lex Debash
  • 31
  • 5
  • 2
    My only guess looking at this would be the `mapView`. You are capturing a reference to this object in your closure. You could try including it in your capture list, e.g. `[unowned self, unowned mapView]`. Otherwise, your leak may simply be in CoreLocation itself. Surprise: iOS is not perfect software and contains leaks of its own. – dalton_c Apr 16 '19 at 11:52
  • Unfortunately didn't help – Lex Debash Apr 16 '19 at 12:06
  • Check your code you are doing placemark?.location else { return }, so if it returns from here then your allocated object let annotation = MKPointAnnotation() is of no use. So to avoid this, allocate object below placemark?.location else { return } condition I think then leak will go. – vivekDas Apr 16 '19 at 12:23

1 Answers1

0

Try replacing :

mapView.showAnnotations([annotation], animated: true)
mapView.selectAnnotation(annotation, animated: true)

With :

weak var weakMapView = mapView
weakMapView.showAnnotations([annotation], animated: true)
weakMapView.selectAnnotation(annotation, animated: true)

Let me know if it helped, if so I will add an explanation.

Vadim F.
  • 881
  • 9
  • 21
  • In the parameter of this method, I substitute the outlet, so it makes no sense to declare an additional property with a weak reference since the outlet already has a weak reference. But I checked your way, it didn't help. – Lex Debash Apr 16 '19 at 14:38
  • My mistake, didn't notice – Vadim F. Apr 16 '19 at 14:41