Using Unreal Engine SetTimer inside OnStartGameSession callback crash dedicated server

Hello everybody,

I’m currently working on an online multiplayer game using AWS GameLift. Everything worked fine until I added a SetTimer in a GameMode GameLift callback (OnStartGameSession).

void AgmodeDM::OnStartGameSession_GameLift(Aws::GameLift::Server::Model::GameSession gameSession) { 
#if WITH_GAMELIFT
    FString Json = gameSession.GetMatchmakerData(); TSharedPtr<FJsonObject> JsonObject;
    TSharedRef<TJsonReader<>> Reader = TJsonReaderFactory<>::Create(Json); 
    if (FJsonSerializer::Deserialize(Reader, JsonObject) && JsonObject->HasField("teams")) {
       const TArray<TSharedPtr<FJsonValue>> Arr_Teams = JsonObject->GetArrayField("teams"); 
       for (int i = 0; i < Arr_Teams.Num(); i++) {
           const TSharedPtr<FJsonObject> TeamJsonObject = Arr_Teams[i]->AsObject(); 
           const TArray<TSharedPtr<FJsonValue>> Arr_Players = TeamJsonObject->GetArrayField("players"); 
           for (int j = 0; j < Arr_Players.Num(); j++) {
               this->Arr_PlayerDatas.Add(FPlayerData(*Arr_Players[j]->AsObject().Get()));
           }
       }
   } 
   // Line below cause the crash 
   GetWorld()->GetTimerManager().SetTimer(
       ShutdownTimerHandle, 
       FTimerDelegate::CreateUObject(
           this, &AgmodeDM::Toto),
        this->fWaitPlayersToJoinTime, false);

Aws::GameLift::Server::ActivateGameSession();

#endif 
}

The crash generate only this line in logs: LogWindows: Windows GetLastError: The operation completed successfully. (0)

In the windows event viewer I found: KERNELBASE.dll, Exception code: 0x00004000

You’ll find below all the things I tried/found to remove the error:

  1. The line causing the crash execute properly in the GameMode begin play function (within or whithout WITH_GAMELIFT)
  2. I tried to recompile everything without success
  3. I copied/pasted the .pdb to dedicated server to add some stack trace in the log, but sadly nothing more in it.

There’s not so much resources on this kind of error thus I decided to open a post here to find some help ! :slight_smile:

Thanks

Its not that error you think it is. Recheck log history more detailed.

I suppose problem with the timer inside of this function is because its running non in UE4 Game Thread, while timers are only allowed there.
Try call Async() function to pass your timer registration to Game Thread. That should help.

Thanks, for your tip used async function did the trick !

Below code if anybody need it in the future.

Async(EAsyncExecution::TaskGraphMainThread, [&]{ StartWaitForPlayerTimer(); });

	Aws::GameLift::Server::ActivateGameSession();
#endif
}

void AgmodeDM::StartWaitForPlayerTimer() {
	GetWorld()->GetTimerManager().SetTimer(
		ShutdownTimerHandle,
		FTimerDelegate::CreateUObject(
			this,
			&AgmodeDM::OnTerminate_GameLift),
		this->fWaitPlayersToJoinTime,
		false);
}

Thanks a lot for your help !