The game cracshes on JoinSession function

Hi, I was trying to make a multiplayer system along with a tutorial but in the JoinSession section my game crashes.
Here is my code:

void AMultiplayerShooterCharacter::OnFindSessionComplete(bool bWasSuccessful)
{
	if(!OnlineSessionInterface.IsValid()) return;
	if(bWasSuccessful && SearchSettings->SearchResults.Num() > 0)
	{
		if(GEngine)
		{
			GEngine->AddOnScreenDebugMessage(
				-1,
				15.f,
				FColor::Green,
				FString::Printf(TEXT("Sessions Found:"))
				);
		}
		
		for (auto Result : SearchSettings->SearchResults)
		{
			FString Id = Result.GetSessionIdStr();
			FString User = Result.Session.OwningUserName;

			FString MatchType;
			Result.Session.SessionSettings.Get(FName("MatchType"),MatchType);
			if(MatchType == FString("FreeForAll"))
			{
				if (GEngine)
				{
					GEngine->AddOnScreenDebugMessage(
						-1,
						15.f,
						FColor::Cyan,
						FString::Printf(TEXT("Joining Match Type: %s ..."), *MatchType)
					);
				}
				
				OnlineSessionInterface->AddOnJoinSessionCompleteDelegate_Handle(OnSessionJoinedDelegate);

				ULocalPlayer* LocalPlayer = GetWorld()->GetFirstLocalPlayerFromController();
				OnlineSessionInterface->JoinSession(*LocalPlayer->GetPreferredUniqueNetId(), NAME_GameSession, Result);	//Here the game crashes
			}
			
			if(GEngine)
			{
				GEngine->AddOnScreenDebugMessage(
					-1,
					15.f,
					FColor::Blue,
					FString::Printf(TEXT("ID: %s | Owner: %s"),*Id,*User)
					);
			}
		}
	}
	else
	{
		if(GEngine)
		{
			GEngine->AddOnScreenDebugMessage(
				-1,
				15.f,
				FColor::Red,
				FString::Printf(TEXT("Couldn't find any session"))
				);
		}
	}
}

It’s the Engine.ini content:

[/Script/EngineSettings.GameMapsSettings]
GameDefaultMap=/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap
EditorStartupMap=/Game/ThirdPersonCPP/Maps/ThirdPersonExampleMap
GlobalDefaultGameMode="/Script/MultiplayerTest.MultiplayerTestGameMode"

[/Script/IOSRuntimeSettings.IOSRuntimeSettings]
MinimumiOSVersion=IOS_12

[/Script/HardwareTargeting.HardwareTargetingSettings]
TargetedHardwareClass=Desktop
AppliedTargetedHardwareClass=Desktop
DefaultGraphicsPerformance=Maximum
AppliedDefaultGraphicsPerformance=Maximum

[/Script/Engine.Engine]
+ActiveGameNameRedirects=(OldGameName="TP_ThirdPerson",NewGameName="/Script/MultiplayerTest")
+ActiveGameNameRedirects=(OldGameName="/Script/TP_ThirdPerson",NewGameName="/Script/MultiplayerTest")
+ActiveClassRedirects=(OldClassName="TP_ThirdPersonGameMode",NewClassName="MultiplayerTestGameMode")
+ActiveClassRedirects=(OldClassName="TP_ThirdPersonCharacter",NewClassName="MultiplayerTestCharacter")

[/Script/Engine.GameEngine]
+NetDriverDefinitions=(DefName="GameNetDriver",DriverClassName="OnlineSubsystemSteam.SteamNetDriver",DriverClassNameFallback="OnlineSubsystemUtils.IpNetDriver")

[OnlineSubsystem]
DefaultPlatformService=Steam

[OnlineSubsystemSteam]
bEnabled=true
SteamDevAppId=480
bInitServerOnClient=true

[/Script/OnlineSubsystemSteam.SteamNetDriver]
NetConnectionClassName="OnlineSubsystemSteam.SteamNetConnection"

and the crash message:

in my tests every thing except joining was OK, the session creates and also when I search I can find the created session but when I try to join it crashes.

I will attach the corresponding files as well.
DefaultEngine.ini (1.4 KB)
MultiplayerTestCharacter.h (3.1 KB)
MultiplayerTestCharacter.cpp (11.8 KB)
MultiplayerTest.Build.cs (427 Bytes)

GetPreferredUniqueNetId() seems to return an invalid UniqueNetId. And operator* on the underlying SharedPtr verifies that the pointer is valid before it is accessed. Not sure when that can happen, there seems to be some caching in place (but only if the cached UniqueNetId is valid). You may have to step through the code to see where it fails to determine the actual UniqueNetId.

Since this is already in your Character class you could also try GetPlayerState()->GetUniqueId() instead, but it may not be valid either.

You should check if the “Result” is valid before you attempt to use it for Joining.

Result.IsSessionInfoValid()

It didn’t work and the crash is still the same.
I checked the NetId and it prints correctly on the screen

I used this condition but it returns true and the crash happens again
here is the edited code:

for (auto Result : SearchSettings->SearchResults)
		{
			FString Id = Result.GetSessionIdStr();
			FString User = Result.Session.OwningUserName;

			FString MatchType;
			Result.Session.SessionSettings.Get(FName("MatchType"),MatchType);
			if(MatchType == FString("FreeForAll"))
			{
				if(!Result.IsSessionInfoValid())
					return;
				if (GEngine)
				{
					GEngine->AddOnScreenDebugMessage(
						-1,
						15.f,
						FColor::Cyan,
						FString::Printf(TEXT("Joining Match Type: %s ..."), *MatchType)
					);
				}
				
				OnlineSessionInterface->AddOnJoinSessionCompleteDelegate_Handle(OnSessionJoinedDelegate);

				auto NetId = GetPlayerState()->GetUniqueId();
				OnlineSessionInterface->JoinSession(*NetId, NAME_GameSession, Result); //Game crashes
			}
			
			if(GEngine)
			{
				GEngine->AddOnScreenDebugMessage(
					-1,
					15.f,
					FColor::Blue,
					FString::Printf(TEXT("ID: %s | Owner: %s"),*Id,*User)
					);
			}
		}

I don’t know have but I fix it :smile:
The actions I did:

  1. I deleted SessionSettings->bAllowJoinViaPresence & SessionSettings->bUsesPresence
  2. As @UnrealEverything said I changed UniqenetId function
  3. As @GarnerP57 said I added IsSessionInfoValid condition
  4. In the ProjectSettings->AssetManager->Primary Asset Types To Scan I added Maps and map directories

And when I tried again it was working fine and now I am able to join to my other device with out any problem.

Thank you all