ChartsOrg / Charts

Beautiful charts for iOS/tvOS/OSX! The Apple side of the crossplatform MPAndroidChart.
Apache License 2.0
27.43k stars 5.97k forks source link

LineChartRenderer.drawHighlighted crash #5154

Open abbottkilroy opened 4 months ago

abbottkilroy commented 4 months ago

What did you do?

We've been seeing A LOT of crashes reported in LineChartRenderer.drawHighlighted.

What did you expect to happen?

No crashes

What happened instead?

It crashes. We've finally got a stack trace that is the probable cause (invalid index in getting lineData[high.dataSetIndex]). We've been unable to reproduce this "on demand".

DGCharts Environment

DGCharts 5.0.0 Xcode version: 15.2 Platform(s) running DGCharts: iOS

Screenshot 2024-03-04 at 11 55 51 AM
y2ducky commented 3 months ago

Same here. DGCharts 5.1.0 Lots of crash reports but I cannot reproduce it.

EXC_BREAKPOINT 0x0000000102cb0b54
Crashed: com.apple.main-thread
0  DGCharts                       0x84b54 $s8DGCharts17LineChartRendererC15drawHighlighted7context7indicesySo12CGContextRefa_SayAA9HighlightCGtF + 1336
1  DGCharts                       0x84c20 $s8DGCharts17LineChartRendererC15drawHighlighted7context7indicesySo12CGContextRefa_SayAA9HighlightCGtFTo + 88
2  DGCharts                       0x242fc $s8DGCharts20BarLineChartViewBaseC4drawyySo6CGRectVF + 2116
3  DGCharts                       0x246fc $s8DGCharts20BarLineChartViewBaseC4drawyySo6CGRectVFTo + 64
y2ducky commented 3 months ago

I've found a workaround that seems to solve the issue. The solution involves calling chartView.highlightValues(nil) every time the chartView's data is changed. This approach seems to prevent crashes by ensuring that any previous highlight indices are cleared before attempting to use them again, which could be the source of the error. Here's an example of how to apply this workaround:

lineChartView.data = newData
lineChartView.highlightValues(nil) // Added line
...
lineChartView.highlightValue(highlight)

I came across this solution by referencing a similar issue discussed here: https://github.com/ChartsOrg/Charts/issues/4024. It appears that the crashes may be related to attempts to use highlight indices from previous data sets, and resetting the highlight values upon data update prevents this issue.