Crash: UPrimitiveComponent::UpdateOverlaps accessing Array of length 0

I don’t believe you are doing anything “wrong.” I suspect that you are just setting up the character in a way that we didn’t expect, so the source code doesn’t handle the situation the way you expect it to. And a crash is certainly not the way we expect it to handle the situation. This is one of the unfortunate side effects of developing any kind of software. As soon as you give it to people to start using, they start doing things with it that were never anticipated or planned for. Sometimes it works out fine, sometimes you get really strange behavior or crashes.

I most likely won’t be able to put much time into this issue today, but I am hoping to at least get started with narrowing it down.

There is no rush whatsoever, we are in over our heads with completely differeny things now. Good luck!

Hi DamirH,

With the help of an extra pair of eyes, I believe I was able to track down where things were going wrong and causing the crash you were experiencing. The character setup that you are using is fine, and actually was a bit of a false trail. The actual culprit lies in the OnOverlapEnd() function of your ProjectileBase class. While turning off the collision on your character’s weapons, the Engine loops through all of the overlapping actors and turns off collision for them one at a time. However, towards the end of that process the OnOverlapEnd() function is being called by a delegate and that is where things go wrong.

The process the Engine goes through when turning off collision for overlapping objects is to generate a TArray of all overlapping objects. It then starts with the last element of the array and loops through the array by turning off collision on the last element and removing it from the array, then decrementing the loop index. This would normally work fine, except when the OnOverlapEnd() function is called, an extra element ends up being removed from the array which results in the loop index looking for an array element that is now out of bounds. When I commented out the code you have in OnOverlapEnd(), there was no crash and the character rag-dolled as expected. I suspect that you just need an extra check in this function to see if it is being called due to normal use of the projectile, or because the character is now dead.

I must admit I am baffled. When I read your reply I facepalmed, realizing I completely forgot that my ProjectileBase has an OnOverlapEnd()… but when I sat down on my PC and saw the function I was left with just bewilderment… because the only thing I do there is turn collision ON (for when my projectile leaves my enemy, to begin colliding with it).

Needless to say, I am a bit confused. I will however add a check to that function to make sure the owner of the weapon is alive and / or if the projectile is being fired.

I cannot express how much your help on this is appreciated, and I can only imagine the frustration you went through to get to the bottom of it. Subsequently, I can’t thank you enough for going through all the trouble. Terrific job and my utmost gratitude and highest of fives to both you and your second pair of eyes.

Best regards,
Damir H.

Admittedly, delegates is a bit of a weak area for me. I was able to trace things through to where the delegates were being called, and noticed that that seemed to be where things were going bad. That was why I wanted to get another set of eyes looking at it to help me track it through the delegate calls.

The process that I was describing earlier about the Engine looping through the overlapping components, turning off collision, then removing the component from the array happens in UpdateOverlaps() in the UPrimitiveComponent class. When you turn the projectile’s collision on in OnOverlapEnd(), the code goes through UpdateOverlaps() again. This ends up clearing out the overlap array that the loop is still using.

Now that I know what was causing the crash, I am hoping to find some time to put together a simplified example of the crash that we can use to find a way to guard against this in the source code.

Oh, now I understand! Thank you so much for clearing that up. Wow… I must admit this was probably the most obscure bug I have ever encountered / caused. You’re a champ ! I hope this didn’t give you too much of a headache at work, and I hope that at least something good will come out of this (the source code safeguard) so that it wasn’t in vain. :stuck_out_tongue:

Thank you!