iOS 计时器函数堆损坏



所以我是iOS的新手,并且正在尝试使用带有定期更新UI的计时器的视图控制器。 我看到的问题是我遇到堆损坏,更具体地说EXC_BAD_ACCESS KERN_INVALID_ADDRESS是由objc_retain调用引起的错误。

此错误发生在多个地方,但都在我的 Timer 函数中,并且在调用堆栈__CFRUNLOOP_IS_CALLING_OUT_TO_A_TIMER_CALLBACK_FUNCTION更高 在每种情况下都被调用。

我一定缺少引用或没有正确发布某些内容,这是代码

func scheduleTimer() {
timer = Timer.scheduledTimer(timeInterval: timeInterval, target: self, selector: #selector(self.timerFunc), userInfo: nil, repeats: true)
}
func timerFunc() {
if let gps = sdlService?.getLatestLocation() {
let clCoor = CLLocationCoordinate2D(locStruct: gps)
self.updateLatestDriverIcon(gps: gps, coor: clCoor)
if isRecording {
self.addNextPathPoint(coor: clCoor)
}
latestCoor = clCoor
}
}
func updateLatestDriverIcon(gps: LocationStruct, coor: CLLocationCoordinate2D) {
if latestCoor == nil {
car = MarkerAnnotation(coordinate: coor, title: carMarker)
mapView.addAnnotation(car!)
latestCoor = coor
mapView.centerOnLatestGPS(animated: false)
markerView.rotation = MathUtils.wrap(gps.bearing, min: 0, max: 360)
} else if coor.isDifferent(to: latestCoor!) {
if isMapFollowingCar {
mapView.centerOnLatestGPS(animated: false)
}
car!.coordinate = coor
markerView.rotation = MathUtils.wrap(gps.bearing, min: 0, max: 360)
}
}

现在,此计时器函数引用我的视图控制器的属性,以及嵌套函数(updateLatestDriverIcon(。 我已经在mapView.centerOnLatestGPS((函数上看到了崩溃,以及markerView.rotation调用堆栈中的多个位置,所有这些都具有上面列出的相同错误代码。 我在这里错过了什么?

编辑: 这是来自崩溃的堆栈跟踪。 我正在使用通过外部附件触发的事件,以便我可以附加到调试器: 堆栈跟踪

因此,经过几周的追踪,我们发现这是由于UIView上的动画。 不确定为什么它会在它的地方抛出错误,如果有人知道为什么这会非常有帮助! 以下是有关体系结构的更多信息:

我们有一个以大约 10HZ 的速度更新 UI 的屏幕,并由使用上述代码的计时器驱动。 动画是在 UIView 子类上完成的,该子类是在呈现到位图上下文的主线程之外完成的。 这是在~30Hz下完成的。

动画代码:

UIView.animate(
withDuration: self.animationDuration,
animations: { self.currentGearValue = actualGearValue },
completion: { (isComplete) in  /* not sure we need this yet */ })

我还没有测试过它,但可能是因为如果下一个动画在下一个动画开始时没有完成,则动画是重叠的。

最新更新