Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fatal error: Index out of range from subscript(position: Index) -> Element #4024

Closed
1 task done
justinisdumb opened this issue Jun 12, 2019 · 9 comments
Closed
1 task done

Comments

@justinisdumb
Copy link

justinisdumb commented Jun 12, 2019

What did you do?

I have an application that is live updating a chart. Its getting temperature values once per second and graphing them. After about 15 Hours I am getting a crash. Fatal error: Index out of range.
Crash is coming from ChartDataSet.swift file.

public subscript(position: Index) -> Element {
    get {
        // This is intentionally not a safe subscript to mirror
        // the behaviour of the built in Swift Collection Types
        return entries[position]    <------------Crash is here.
    }
    set {
        calcMinMax(entry: newValue)
        entries[position] = newValue
    }
}

}

My 'position' value at this point is 3754, so I know it has a value.
My 'entries' array has 4364 values in it at this point and the 3754 location does have data in it.

What did you expect to happen?

This should not be out of range. The entries array length is much longer than 3754 and it has values in it.

What happened instead?

Fatal error: Index out of range

Charts Environment

Charts version/Branch/Commit Number:. Version 3.3.0
Xcode version:. 10.2.1
Swift version: 4.2
Platform(s) running Charts: Mac OSX Mojave
macOS version running Xcode:. 10.14.5

@liuxuan30
Copy link
Member

I think you should check your data first, I'm not sure if it's our bug. If you sure, you should provide data code so we can reproduce with ChartsDemo.

Otherwise we will think it's your issue, as OOB could go very wild.

reopen when you have details we could reproduce

@Nuthen
Copy link

Nuthen commented Aug 9, 2019

I've been dealing with this issue all week and finally found a fix, so hopefully I can help.

I'm working on a similar app and getting the same crash.
I eventually learned I could stop the crash from happening by deleting old entries in the graph.
After about 10,000 entries, the crash occurs. Not all entries are shown at the same time due to having a set max visibility, so it doesn't cause the app to actually slowdown, that's what made it tough to figure out.

It can be reproduced by adding 10,000 entries to the chart right away (I was using a line graph and 3 data sets), and the crash happens soon (or immediately) after. I'm running my app on a really old iPhone.

But I think the library has an issue with some internal function when you get this many entries, where it slows down and tries to get an invalid index after waiting too long. That's just a guess.

I fixed it by deleting old entries so that the number didn't pile up, and kept it limited to a certain max number of entries.

In my case, I was trying to add 30 entries per second, and it would crash after only 5 minutes, so thankfully having the crash in only 5 minutes helped me to figure out what was causing this.

I hope that helps.

I'm not sure what the best way to fix that would be, but at least a warning when there are too many entries for it to handle would have saved me a lot of headache.

@tnmendes
Copy link

I am also having issue in the same place, here is a print screen:

Screenshot 2023-01-10 at 12 23 05

@sasicse990
Copy link

I also have the same issue as @tnmendes mentioned

@tnmendes
Copy link

tnmendes commented Jan 10, 2023

I think i found the reason, before i do the setChartData() i am running this function clearChartValues().

private func clearChartValues() {
        
        if(!self.isEmpty()){
            self.clearValues()
        }
    }

After I removed this function it is no longer happening.
it's a bit more harder to happen but it still happens :(

@tnmendes
Copy link

tnmendes commented Jan 10, 2023

Thread 0 Crashed::  Dispatch queue: com.apple.main-thread
0   libswiftCore.dylib            	       0x18bc5923c closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 356
1   libswiftCore.dylib            	       0x18bc5923c closure #1 in closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 356
2   libswiftCore.dylib            	       0x18bc58fa4 closure #1 in closure #1 in _assertionFailure(_:_:file:line:flags:) + 192
3   libswiftCore.dylib            	       0x18bc58990 _assertionFailure(_:_:file:line:flags:) + 228
4   libswiftCore.dylib            	       0x18bc3a2e4 _ArrayBuffer._checkInoutAndNativeTypeCheckedBounds(_:wasNativeTypeChecked:) + 276
5   libswiftCore.dylib            	       0x18bc3e01c Array.subscript.getter + 84
6   Watt for Smart Devices        	       0x102a374f4 ChartData.subscript.getter + 100 (ChartData.swift:452)
7   Watt for Smart Devices        	       0x102aad4d4 LineChartRenderer.drawHighlighted(context:indices:) + 552 (LineChartRenderer.swift:763)
8   Watt for Smart Devices        	       0x102aaded4 @objc LineChartRenderer.drawHighlighted(context:indices:) + 92
9   Watt for Smart Devices        	       0x1029be4ec BarLineChartViewBase.draw(_:) + 3944 (BarLineChartViewBase.swift:251)
10  Watt for Smart Devices        	       0x1029bec08 @objc BarLineChartViewBase.draw(_:) + 100
11  UIKitCore                     	       0x107b01610 -[UIView(CALayerDelegate) drawLayer:inContext:] + 468
12  QuartzCore                    	       0x187d60854 CABackingStoreUpdate_ + 200
13  QuartzCore                    	       0x187ed916c invocation function for block in CA::Layer::display_() + 52
14  QuartzCore                    	       0x187ecfcdc -[CALayer _display] + 1684
15  QuartzCore                    	       0x187ee205c CA::Layer::layout_and_display_if_needed(CA::Transaction*) + 392
16  QuartzCore                    	       0x187e117d0 CA::Context::commit_transaction(CA::Transaction*, double, double*) + 452
17  QuartzCore                    	       0x187e3d47c CA::Transaction::commit() + 652
18  QuartzCore                    	       0x187e3e8b0 CA::Transaction::flush_as_runloop_observer(bool) + 68
19  CoreFoundation                	       0x1803700e4 __CFRUNLOOP_IS_CALLING_OUT_TO_AN_OBSERVER_CALLBACK_FUNCTION__ + 32
20  CoreFoundation                	       0x18036aa84 __CFRunLoopDoObservers + 512
21  CoreFoundation                	       0x18036af3c __CFRunLoopRun + 968
22  CoreFoundation                	       0x18036a75c CFRunLoopRunSpecific + 584
23  GraphicsServices              	       0x188f60c98 GSEventRunModal + 160
24  UIKitCore                     	       0x10766cb74 -[UIApplication _run] + 868
25  UIKitCore                     	       0x107670b1c UIApplicationMain + 124
26  Watt for Smart Devices        	       0x1026cf3a8 main + 64 (AppDelegate.swift:22)
27  dyld_sim                      	       0x10386dfa0 start_sim + 20
28  dyld                          	       0x10395d08c start + 520

@tnmendes
Copy link

I finally found the problem and how to replicate and solution :D

Analyzing the last crash I understood that the problem was the highlight value on the chart:
Print screen last crash:
Screenshot 2023-01-14 at 19 40 03

I have a graph where the x is timeIntervalSince1970 and the granularity in 3 of them is daily and in the last tab it is monthly.
You can see in the chart that I have a highlight on the 13th day, the problem is when I click on the Year tab and the chart tries to take that same highlight to the new dataset with completely different X values and it crash!!!
Screenshot 2023-01-14 at 20 38 10

The solution to this problem is when you are doing a set chart with new data, force it to chartView.highlightValues(nil). This will deselect all the highlight.

@liuxuan30 Do you think you can do something to prevent this from happening?

@hungnb94
Copy link

The solution to this problem is when you are doing a set chart with new data, force it to chartView.highlightValues(nil). This will deselect all the highlight.

Thank you so much, I have the same problem on Charts version 5.0.0.
It solve my problem 🥳

@sala7khaled
Copy link

I was facing the same fatal error while using bar and line charts on the same screen and I frequently updated its data source, while I also added some markers on both, but @tnmendes 's solution just saved my day!

I added those lines before updating the data source.

 barChartView.highlightValues(nil)
 lineChartView.highlightValues(nil)
        
 barChartView.marker = nil
 lineChartView.marker = nil

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants