Recently my project has had pretty spontaneous crashes. These crashes are random and inconsistent. I can’t seem to replicate them if I tried, unfortunately. However, they all seem to relate to a single UAudioComponent that is inside one of my c++ classes.
Here is the crash that keeps appearing:
Assertion failed: Index >= 0 [File:C:\Program Files\Epic Games\UE_4.25\Engine\Source\Runtime\CoreUObject\Public\UObject/UObjectArray.h] [Line: 699]
UE4Editor_Core
UE4Editor_Core
UE4Editor_KartRacer_0009!AKart::playSound() [C:\Users\OneDrive\Documents\Unreal Projects\KartRacer\Source\KartRacer\Kart.cpp:92]
UE4Editor_KartRacer_0009!AKart::Tick() [C:\Users\OneDrive\Documents\Unreal Projects\KartRacer\Source\KartRacer\Kart.cpp:162]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_UnrealEd
UE4Editor_UnrealEd
UE4Editor
UE4Editor
UE4Editor
UE4Editor
UE4Editor
kernel32
ntdll
From what you can see here, the problem arises inside a helper function I created named playSound().
Here is the code for that function:
void AKart::playSound(USoundCue *s)
{
if (IsValid(audioComponent))
{
audioComponent->Stop();
}
audioComponent = UGameplayStatics::SpawnSound2D(this, s);
}
FYI, the line that is given in the crash, 92, is the line “if (IsValid(audioComponent))”.
The provided function is the ONLY function in the c++ class where the UaudioComponent is manipulated in any way. And the fact that this does not happen consistently in any way, makes this much more aggravating.
Could this possibly be a bug with unreal? Or am I misusing UAudioComponents? Any help is much appreciated.
This potentially be a bug since during validity check array getting index lower then 0 that only thing i can deduce as you don’t have debug symbols (you can install them) to fully read the stack trace. But i might indeed misuse but i dont have full picture to jurge that.
That said, is there any reason why you need to you use IsValid? IsValid should be used only if there risk of invalid pointer (pointer that is not null (aka 0) but pointing to object to does not exist, technically to non-allocated or trash memory address), normally if you follow conventions of use UObject this should not happen and simple null check should be sufficient. In fact i don’t see IsValid being used much in engine code or else its in some non-UObject zone where you can’t use UPROPERTY.
IsValid is there just to make sure I don’t try to stop audioComponent when it isn’t set to anything. As I said, this is the only place in the code where audioComponent is manipulated in any way, so the first time I run playSound in a play session, the game crashes without isValid because of what I assume to be because audioComponent is not set to anything yet.
If there is a better way to achieve this that may help my problem, I would love to know.
If this does happen to be a bug with unreal, I could try a try/catch as a temporary solution maybe?
I did some fiddling around, and I’ve come across a new crash that may be more helpful.
By changing my playSound function to the following:
if (audioComponent != nullptr)
{
audioComponent->DestroyComponent();
}
audioComponent = UGameplayStatics::SpawnSound2D(this, s);
I now get a crash with
Unhandled Exception: EXCEPTION_ACCESS_VIOLATION 0xe46c0fa
This crash is just as random as the previous, so I believe it to be related to the original error. Clearly, there is some memory address that it is trying to access, but can’t.
Also note, the line that is causing the crash now is the last line of the function, which makes it even stranger. Unsure how a memory access violation can be caused when I’m not even trying to access the audioComponent.
It’s a possibility it could be SoundCue *s that is the problem, but the variables that are passed in are ONLY constants, so there is no way it could be something unknown to me.
Any suggestions on how to fix memory access problems like this? I am stumped.
Late response but I figured out the problem a couple days ago!
The issue was garbage collection, and that I also forgot to add UPROPERTY() above my decratation of the AudioComponent in my header file. That should prevent any crashes for someone who may be having the same problem in the future!