SpawnActor returning nullptr

Hi fellows, I’m making a game using C++ and blueprints, but I’m stuck on an access violation error. It doesn’t happens all the time, it’s something random, but always on the same line of the code. The stack of the error is:

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

UE4Editor_Engine!APawn::GetController() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\pawn.cpp:433]
UE4Editor_Squad51_663!UEnemySpawner::ExecuteAction() [c:\users\marci_000\projects\squad51\source\squad51\enemies\enemyspawner.cpp:10]
UE4Editor_Squad51_663!USquadMemberSpawner::ExecuteAction() [c:\users\marci_000\projects\squad51\source\squad51\enemies\squadspawner.cpp:9]
UE4Editor_Squad51_663!AEventsTimeLine::ExecuteCurrentAction() [c:\users\marci_000\projects\squad51\source\squad51\eventstimeline.cpp:64]
UE4Editor_Engine!FTimerUnifiedDelegate::Execute() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\public\timermanager.h:38]
UE4Editor_Engine!FTimerManager::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\timermanager.cpp:439]
UE4Editor_Engine!UWorld::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\engine\private\leveltick.cpp:1212]
UE4Editor_UnrealEd!UEditorEngine::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\editor\unrealed\private\editorengine.cpp:1347]
UE4Editor_UnrealEd!UUnrealEdEngine::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\editor\unrealed\private\unrealedengine.cpp:361]
UE4Editor!FEngineLoop::Tick() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\launchengineloop.cpp:2427]
UE4Editor!GuardedMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\launch.cpp:142]
UE4Editor!GuardedMainWrapper() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\windows\launchwindows.cpp:126]
UE4Editor!WinMain() [d:\buildfarm\buildmachine_++depot+ue4-releases+4.10\engine\source\runtime\launch\private\windows\launchwindows.cpp:200]
UE4Editor!__scrt_common_main_seh() [f:\dd\vctools\crt\vcstartup\src\startup\exe_common.inl:264]
kernel32
ntdll

And this is my function, where the error occurs (on Pawn->GetController()):

void UEnemySpawner::ExecuteAction(FTimeLineEvent TimeLineEvent)
{
	FActorSpawnParameters Parameters;
	FTransform Transform = CalculateTransform();
	APawn* Pawn = (APawn*)TimeLineEvent.OwnerTimeLine->GetWorld()->SpawnActor(EnemyClass, &Transform, Parameters);
	AEnemyAIController* AIController = (AEnemyAIController*)Pawn->GetController();
	if (AIController != nullptr && AIController->IsA<AEnemyAIController>())
	{
		AIController->MovementSettings = MovementSettings;
		AIController->DirectionAngle = CalculateDirectionAngle();
	}
	AfterSpawn(Pawn);
}

FTimeLineEvent is a struct of my project, AEnemyAIController is a class that extends AAIController (and it is setted by default on the blueprint; like I said before, sometimes it works, so I’m sure the controller is setted).

This function is called inside a callback of a timer, can it be the cause of the issue?

Unfortunately, i’m also not able to use a breakpoint to investigate because it doesn’t work either… but I think it is something for another thread.

  • Sorry for my bad english, but for me it’s hard to explain this issue even in my native language :slight_smile:

Edit: I changed the title of the question to the real issue. Otherwise, some people with the same issue could not find it.

Well, just some minutes after I posted the question, I figured out one thing: the problem is the SpawnActor function. It is returning null sometimes, but I don’t know why. I added an “if” for safety and some parameters to ignore the collision, but I still don’t know why sometimes the actor is not being spawned.

void UEnemySpawner::ExecuteAction(FTimeLineEvent TimeLineEvent)
{
	FActorSpawnParameters Parameters;
	Parameters.bNoCollisionFail = true;
	Parameters.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
	FTransform Transform = CalculateTransform();
	APawn* Pawn = (APawn*)TimeLineEvent.OwnerTimeLine->GetWorld()->SpawnActor(EnemyClass, &Transform, Parameters);
	if (Pawn != nullptr)
	{
		AEnemyAIController* AIController = (AEnemyAIController*)Pawn->GetController();
		if (AIController != nullptr && AIController->IsA<AEnemyAIController>())
		{
			AIController->MovementSettings = MovementSettings;
			AIController->DirectionAngle = CalculateDirectionAngle();
		}
		AfterSpawn(Pawn);
	}
}

Hello Marcio,

I’m not exactly sure as of yet exactly what is causing that to occur but I do have one good practice for when spawning actors that can avoid other issues like this. When you’re spawning an actor, it requires a UWorld to do it with. If that UWorld is NULL, an assert will be thrown. To avoid this happening, surround the SpawnActor call in an if statement with a condition such as:

if(GetWorld())

That way, if GetWorld returns null, which could happen in some cases, the assert will be avoided.

Thanks for your response, . But actually it doesn’t fix my problem, because GetWorld() is working fine. Like I said on my second post, the problem is the SpawnActor function. It is returning nullptr sometimes and I don’t know why. And I didn’t find any information about it in the docs…

FIXED.

I opened the output log. It was there, the Rotator had invalid values (NaN). It was an obvious thing to do, but I didn’t. Shame on me.

And why the values are NaN? Because the default constructor of the FRotator (and even the FVector) doesn’t initialize the internal values. It is in the docs.

To fix it, I just initialize the FRotator and FVector using FRotator(0) and FVector(0).