标注中的内存泄漏?

时间:2018-10-30 21:34:04

标签: ios swift xcode instruments

在过去的两个月左右的时间里,我一直在学习Swift。仪器说每次我在mapView上调用注释时都会发生内存泄漏。我假设它与我用于注解的类有关,但我不确定(当我加载注解时,仪器不会显示泄漏,并且每当我尝试将var pointOfInterest更改为弱或无所有权时) ,xcode使我不得不解开一切)。

我会在其中一个类中不使用弱变量或无主变量的情况下发生内存泄漏吗?

我没有足够的积分来发布图像,但是仪器显示我的泄漏点:

  • CGDataProvider
  • CFData
  • CGImage
  • CFAllocator

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

  • Malloc +1 1: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
        }

    }
}

}

0 个答案:

没有答案