Array index out of bounds error from line of code which does not contain an array

when I run the editor from the debugger the call stack encounters an array index out of bounds error from the following line:

newTrackPos = previousTurnOrIntersectionPosition + FVector2D(0, correctedCurrentTurnDist);

the error is “Array index out of bounds: 1 from an array of size 1”

could the error be occuring elsewhere somehow and the call stack is just mistaken? Ive worked with FVector2Ds alot and never seen anything like this

update: still no clucking idea

Greetings @lazyFrond

The old ‘Array index out of bounds’! I’ve danced with this one myself. This error basically occurs when you’re trying to access/grab something from an address that doesn’t belong to the array. For instance if you recently added/removed a file from your project folder and didn’t remove it from inside the project in UE, it will give that error. Basically, it’s calling for something that doesn’t exist. I’d start with backtracking and trying to think of any changes you made just prior to this popping up. That’ll usually lead you to the culprit. I hope that helps explain it some! Also, here’s a post for reference that explains it a bit more: Assertion failed (Index >= 0) & (Index < ArrayNum)

1 Like

Thanks Frosty! Im still kind of new to this development environment, is there any way or any where to find which array specifically caused the error?

I recently had a similar question solved by jwatte. he said:
“You might want to attach to the editor process from Visual Studio. You will then get a complete stack trace and debugger at the point of crash, where you can easily see where the problem is.
Or you can even launch the editor from the debugger.”
I didnt know what he meant by “attach to the editor process” so I just started launching the editor from the debugger which has been working

It’s hard to tell with the limited information that you’ve given, but I am pretty sure the error comes from a point further up in the call stack

In your screenshot you can see that the label can’t be read, which leads me to believe that this is not valid
So somewhere previously in the callstack something was attempted to be read but was out of bounds

And since arrays are nothing but memory addresses, the compiled program doesn’t actually know about the size of the array (this is what the TArray wrapper does, and asserts if you are trying to access something out of bounds), and will just do what you tell it, so if you try to access the 5th element of an array with 4 elements, it will try to do that, and will try to read that memory as what you told it it is, but will fail. So it can continue executing code, until it needs to access something that would be part of the data structure of the array element

You can also wrap the relevant functions in your callstack with PRAGMA_DISABLE_OPTIMIZATION and PRAGMA_ENABLE_OPTIMZATION which might allow you to get more information since the code won’t be optimized and all variables will be available and lines won’t be skipped

But I would say go up your callstack until you find a call that’s being made on an invalid address

1 Like

I will look into pragma_disabled_optimization thank you! About the label error I tried clicking it and it took me to like some deep windows hex code script so im just going to pretend i didnt see that

From this screenshot it really looks like this is not a valid AHUD object, so my guess is that in a previous place in the call stack you are trying to access the HUD from an array of AHUD?

1 Like

the only time im using the “this” keyword is as follows

Screenshot 2024-01-21 090200

Im feeding the hud into the newly created widget as a TWeakObjectPtr it has been working

That’s fine, but the code in your original post’s screenshot, where is that called from? I think that’s where it fails
If you don’t mind post a screenshot of the callstack after crashing

1 Like

sure! the call stack doesnt really help in this case

Screenshot 2024-01-21 092146

this GenerateLevel() is being called at the bottom of the BeginPlay() function

Screenshot 2024-01-21 092325

its probably difficult to help without having the code in front of you but Ive been stumped by this for weeks all assistance is greatly appreciated :pray:

both newTrackPos and PreviousTurnOrIntersectionPosition are FVector2D data type variables

previousTurnOrIntersectionPosition is being set from an array but that array cant possibly go out of bounds Ive checked and double checked, and youd think i tried to set the value for a FVector2D data type variable to be something that is not an FVector2D it would cause an error by itself

by wrapping the relevant code in PRAGMA_DISABLE_OPTIMIZATION the debugger was able to isolate the exact line causing the errors. Thank you zeaf! it was very good talking to you

1 Like

Greeting @lazyFrond

I see that you were able to get that resolved. It’s great to see the community helping each other. Good deal!

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.