Assertion failed: Index >= 0 a while after Destroying referenced Actors

In my prototype, I am iterating over an TArray of structs every Tick. The struct has a pointer to an enemy actor which is used to update their positions around the Player. It’s working fine (I check the pointers for validity before trying to update their positions), but a while after some of the enemies are destroyed (about half a Minute), the editor crashes with this error message:

Assertion failed: Index >= 0 [File:c:\program files (x86)\epic games\4.13\engine\source\runtime\coreuobject\public\uobject\UObjectArray.h] [Line: 402] 

UE4Editor_Core!FDebug::AssertFailed() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\misc\outputdevice.cpp:421]
UE4Editor_Diplomprojekt2016_17_5724!USwarmAIManager::UpdateEnemyPositions() [c:\users\jrbyd\documents\diplomprojekt\projekt\source\diplomprojekt2016_17\swarmaimanager.cpp:53]
UE4Editor_Diplomprojekt2016_17_5724!USwarmAIManager::execUpdateEnemyPositions() [c:\users\jrbyd\documents\diplomprojekt\projekt\source\diplomprojekt2016_17\swarmaimanager.h:42]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:4280]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:697]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2126]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:922]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:821]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:922]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:4280]
UE4Editor_CoreUObject!UObject::ProcessEvent() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1305]
UE4Editor_Engine!AActor::ProcessEvent() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:646]
UE4Editor_Engine!AActor::ReceiveTick() [d:\build\++ue4+release-4.13+compile\sync\engine\intermediate\build\win64\ue4editor\inc\engine\engine.generated.1.cpp:2816]
UE4Editor_Engine!AActor::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:844]
UE4Editor_Engine!AGameMode::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\gamemode.cpp:779]
UE4Editor_Engine!AActor::TickActor() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:831]
UE4Editor_Engine!FActorTickFunction::ExecuteTick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:112]
UE4Editor_Engine!FTickFunctionTask::DoTask() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:251]
UE4Editor_Engine!TGraphTask<FTickFunctionTask>::ExecuteTask() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\public\async\taskgraphinterfaces.h:868]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:932]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:679]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:1776]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:520]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:1429]
UE4Editor_Engine!UWorld::RunTickGroup() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:706]
UE4Editor_Engine!UWorld::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:1275]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1408]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:371]
UE4Editor!FEngineLoop::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:2834]
UE4Editor!GuardedMain() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\launch.cpp:156]
UE4Editor!GuardedMainWrapper() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Code sample:

//Gets called every frame
void USwarmAIManager::UpdateEnemyPositions()
{
	//PlayerSurroundPositions = the TArray

	for (int i = 0; i < PlayerSurroundPositions.Num(); i++)
	{
		if (PlayerSurroundPositions[i].ActorInSlot != nullptr && IsValid(PlayerSurroundPositions[i].ActorInSlot)) //Desperately checking if reference is valid
		{																											
			if (PlayerSurroundPositions[i].ActorInSlot->IsValidLowLevel() && !PlayerSurroundPositions[i].ActorInSlot->IsPendingKill())
			{
				//Update location
				PlayerSurroundPositions[i].ActorInSlot->SetActorLocation(UGameplayStatics::GetPlayerPawn(GetWorld(), 0)->GetActorLocation() + PlayerSurroundPositions[i].PlayerRelativePosition);
			}
		}
	}
}

Interestingly, this does NOT happen if I launch the editor from Visual Studio by debugging.
This issue is pretty irritating and hinders the Progress of our project dramatically, since we try to implement our core mechanic.

UPDATE!!
After testing again, I just noticed that the editor crashes even if I pause the PIE process after destroying one of the enemy actors and clicking on resume or stop after a while.
In this case, I get a different crash report:

Access violation - code c0000005 (first/second chance not available)

UE4Editor_Engine!AActor::SetActorLocation() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:3100]
UE4Editor_Diplomprojekt2016_17_5724!USwarmAIManager::UpdateEnemyPositions() [c:\users\jrbyd\documents\diplomprojekt\projekt\source\diplomprojekt2016_17\swarmaimanager.cpp:56]
UE4Editor_Diplomprojekt2016_17_5724!USwarmAIManager::execUpdateEnemyPositions() [c:\users\jrbyd\documents\diplomprojekt\projekt\source\diplomprojekt2016_17\swarmaimanager.h:42]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:4280]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:697]
UE4Editor_CoreUObject!UObject::ProcessContextOpcode() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:2126]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:922]
UE4Editor_CoreUObject!UObject::CallFunction() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:821]
UE4Editor_CoreUObject!UObject::ProcessInternal() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:922]
UE4Editor_CoreUObject!UFunction::Invoke() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\class.cpp:4280]
UE4Editor_CoreUObject!UObject::ProcessEvent() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\coreuobject\private\uobject\scriptcore.cpp:1305]
UE4Editor_Engine!AActor::ProcessEvent() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:646]
UE4Editor_Engine!AActor::ReceiveTick() [d:\build\++ue4+release-4.13+compile\sync\engine\intermediate\build\win64\ue4editor\inc\engine\engine.generated.1.cpp:2816]
UE4Editor_Engine!AActor::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:844]
UE4Editor_Engine!AGameMode::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\gamemode.cpp:779]
UE4Editor_Engine!AActor::TickActor() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:831]
UE4Editor_Engine!FActorTickFunction::ExecuteTick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\actor.cpp:112]
UE4Editor_Engine!FTickFunctionTask::DoTask() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:251]
UE4Editor_Engine!TGraphTask<FTickFunctionTask>::ExecuteTask() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\public\async\taskgraphinterfaces.h:868]
UE4Editor_Core!FNamedTaskThread::ProcessTasksNamedThread() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:932]
UE4Editor_Core!FNamedTaskThread::ProcessTasksUntilQuit() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:679]
UE4Editor_Core!FTaskGraphImplementation::WaitUntilTasksComplete() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\core\private\async\taskgraph.cpp:1776]
UE4Editor_Engine!FTickTaskSequencer::ReleaseTickGroup() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:520]
UE4Editor_Engine!FTickTaskManager::RunTickGroup() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\ticktaskmanager.cpp:1429]
UE4Editor_Engine!UWorld::RunTickGroup() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:706]
UE4Editor_Engine!UWorld::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\engine\private\leveltick.cpp:1275]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\editorengine.cpp:1408]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\editor\unrealed\private\unrealedengine.cpp:371]
UE4Editor!FEngineLoop::Tick() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\launchengineloop.cpp:2834]
UE4Editor!GuardedMain() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\launch.cpp:156]
UE4Editor!GuardedMainWrapper() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\build\++ue4+release-4.13+compile\sync\engine\source\runtime\launch\private\windows\launchwindows.cpp:202]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

Thanks in advance for your help and advice…

Hey JRB1994-

Can you post the code/blueprint where you’re iterating over the struct array? It sounds as though the crash is caused by attempting to access an array element that no longer exists. If you’re able to reproduce the crash in a new project, can you provide the setup steps or a sample project to help me test the crash locally.

As for now, I added the Code to my question.
I try to reproduce the issue in a new Project later.
Thanks.

The editor crashed again, but this time with a different report.
I added the crash report to my question.

I tried to reproduce the crash in a new project but it seemed to work without problems, no crash.
In the original project, the enemy actors also had an AIController with a BehaviorTree. But I’m not sure if this has anything to do with the crash.

What should I do if we can’t find a solution? Would migrating the project files of the broken project into an empty one work?

Looking at the updated information in the original post, it appears that second crash is caused by attempting to call SetActorLocation in the UpdateEnemyPositions() function. Since the error is an access violation, it is likely that the actor that you are setting the location of can’t be found / doesn’t exist. The original crash also appears to be caused by attempting to access an array element that is outside of the array bounds. Migrating the project files would be one possible solution, however I feel this section of code may cause the crash to occur again after migration. The best way to check this would be to migrate over and test independent sections to see which file is responsible for the crash.

Hmmh, it’s really weird because I don’t add or remove any element from the array during gameplay. I copied the same code into the new Project but no crash happened. I will test if migrating makes any difference.
It would be strange if this error only happens in this project, though.

I added GetWorld()->ForceGarbageCollection(true) to my Code and the editor crashed indeed instantly with the Access Violation error after collecting.
Something I suspected before, so I tried to make sure the referenced actor still exists by doing all those IsValid() and is ValidLowLevel() checks.
Did I miss something…?

Solved it. I built my code with the DebugGameEditor configuration in VS, so the changes I did in my code were not reflected in the editor. Because of this I am not exactly sure which change I did solved my problem though.

I had a similar issue. The game crashed when I destroyed an actor in a TArray but only after a delay. Using ForceGarbageCollection(true) caused the crash to happen immediately.

Looking at the documentation for garbage collection. Unreal handles a lot when it comes to object pointers.

I solved it by putting UPROPERTY() macro above my TArray.

I originally had a small TArray of actors as a private variable in one of my classes. Unreal seems to not like it when a container that contains object pointers is missing a UPROPERTY() macro. Hope this helps someone else.

1 Like