x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

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 :)

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

Product Version: UE 4.10
Tags:
more ▼

asked Dec 03 '15 at 01:38 AM in C++ Programming

avatar image

Marcio Daniel
500 16 33 100

avatar image Marcio Daniel Dec 03 '15 at 02:27 AM

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);
     }
 }
avatar image Matthew J Dec 03 '15 at 10:59 PM

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.

avatar image Marcio Daniel Dec 04 '15 at 12:26 AM

Thanks for your response, Matthew. 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...

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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).

more ▼

answered Dec 04 '15 at 01:57 AM

avatar image

Marcio Daniel
500 16 33 100

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question