Memory Leak in CallOuts?

Viewed 133

been working on learning Swift for the past 2 months or so. Instruments is saying I have memory leaks every time I call out an annotation on my mapView. I'm assuming it has to do with the class I'm using for annotations but I'm not sure (when I load the annotations, instruments doesn't show leaks, and whenever I try to change the var pointOfInterest to weak or unowned, xcode makes me force unwrap everything).

Am I going to have memory leaks without using a weak or unowned var in one of the classes?

I don't have enough points to post images, but instruments says I have leaks with:

  • CGDataProvider
  • CFData
  • CGImage
  • CFAllocator

and when I backtrace one of them (CGDataProvider shown below), everything matches up except for the malloc at the top of the list.

  • Malloc +1 1 00:21.055.530 CoreGraphics CGTypeCreateInstance

  • CFRetain +1 2 00:21.055.540 CoreGraphics CGDataProviderRetain

  • CFRelease -1 1 00:21.055.543 MapKit -[_MKCalloutLayer _newContentImage]

Classes for points and annotations:

class PointOfInterest: NSObject {
    var name: String
    var coordinate: CLLocationCoordinate2D
    var type: String

    init(name: String, coordinate: CLLocationCoordinate2D, type: String) {
        self.name = name
        self.coordinate = coordinate
        self.type = type
    }

}

class POIAnnotation: NSObject, MKAnnotation {
    var pointOfInterest: PointOfInterest
    var coordinate: CLLocationCoordinate2D { return pointOfInterest.coordinate }
    var type: String { return pointOfInterest.type}

    init(point: PointOfInterest) {
        self.pointOfInterest = point
        super.init()
    }

    var title: String? {
        return pointOfInterest.name
    }

    var subtitle: String? {
        return pointOfInterest.type
    }

    var markerTintColor: UIColor  {
        switch type {
        case "Route":
            return .black
        case "Animals":
            return .yellow
        case "General":
            return .cyan
        case "Dining":
            return MyColors.diningGreen
        case "Shopping":
            return MyColors.shoppingRed
        case "Catering":
            return MyColors.cateringOrange
        case "Aviaries":
            return .purple
        case "Restrooms":
            return .blue
        case "Water Fountains":
            return .blue
        default:
            return .gray
        }
    }

    var imageName: String? {
        //these glyphs should scale from 20 to 40px https://icons8.com/
        if type == "Animals" { return "Animal Icon" }
        if type == "Aviaries" { return "Aviary" }
        if type == "Restrooms" { return "Restroom Icon" }
        if type == "Dining" { return "Dining Icon" }
        if type == "Catering" { return "Dining Icon" }
        if type == "Shopping" { return "Shopping Icon" }
        return "Flag"
    }

    var summaryText: String? {

        if let mySummary = Summaries.summaryTextDictionary[pointOfInterest.name] {
            return mySummary
        } else {
            return "No summary found"
        }

    }

}

class PlaceMarkerView: MKMarkerAnnotationView {
override var annotation: MKAnnotation? {
    willSet {
        // 1
        guard let place = newValue as? POIAnnotation else { return }
        canShowCallout = true
        calloutOffset = CGPoint(x: -5, y: 5)
        rightCalloutAccessoryView = UIButton(type: .detailDisclosure)
        displayPriority = .required

        // 2
        markerTintColor = place.markerTintColor
        if let imageName = place.imageName {
            glyphImage = UIImage(named: imageName)
        } else {
            glyphImage = nil
        }

    }
}

}

0 Answers
Related