3

过去 2 个月左右一直致力于学习 Swift。Instruments 说每次我在 mapView 上调用注释时都会发生内存泄漏。我假设它与我用于注释的类有关,但我不确定(当我加载注释时,仪器不会显示泄漏,并且每当我尝试将 var pointOfInterest 更改为 weak 或 unowned , xcode 让我强制解开一切)。

如果在其中一个类中不使用弱或无主变量,我是否会发生内存泄漏?

我没有足够的积分来发布图片,但仪器说我有泄漏:

  • CGDataProvider
  • CFData
  • CG图像
  • 分配器

当我回溯其中一个(CGDataProvider 如下所示)时,除了列表顶部的 malloc 之外,所有内容都匹配。

  • 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]

点和注释的类:

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
        }

    }
}

}

4

0 回答 0