# The game cracshes on JoinSession function

**URL:** <https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289>\
**Category:** Multiplayer & Networking\
**Tags:** online-subsystem, onlinesubsystem, unreal-engine, crash, session, CPP, question\
**Created:** [December 31, 2022, 6:47am UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289 "2022-12-31T06:47:45Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SaJjad1378](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaJjad1378](https://forums.unrealengine.com/u/SaJjad1378)\
**Post date:** [December 31, 2022, 6:47am UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289/1 "2022-12-31T06:47:45Z")

</div>

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:

```auto
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:

```auto
[/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:

 ![image](https://d3kjluh73b9h9o.cloudfront.net/original/4X/e/9/2/e926703cdaa87d9c4bc0bf59140a48970a09331c.png)

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](https://forums.unrealengine.com/uploads/short-url/1nspRLdIiylP2ZMD4HZrjXvC6zi.ini) (1.4 KB)  
[MultiplayerTestCharacter.h](https://forums.unrealengine.com/uploads/short-url/99c8zqSEqv7XzWxWlHH02AQPyjk.h) (3.1 KB)  
[MultiplayerTestCharacter.cpp](https://forums.unrealengine.com/uploads/short-url/g4L3iiUkMDvLewhnpHcQWPyHbjp.cpp) (11.8 KB)  
[MultiplayerTest.Build.cs](https://forums.unrealengine.com/uploads/short-url/i4NuNa1AWBGzcs5yPdXF6meNeQP.cs) (427 Bytes)

---

<div class="post-metadata">

**Author:** ![UnrealEverything](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@UnrealEverything](https://forums.unrealengine.com/u/UnrealEverything)\
**Post date:** [December 31, 2022, 11:15am UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289/2 "2022-12-31T11:15:15Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![GarnerP57](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@GarnerP57](https://forums.unrealengine.com/u/GarnerP57)\
**Post date:** [December 31, 2022, 12:49pm UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289/3 "2022-12-31T12:49:45Z")

</div>

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

```auto
Result.IsSessionInfoValid()

```

---

<div class="post-metadata">

**Author:** ![SaJjad1378](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaJjad1378](https://forums.unrealengine.com/u/SaJjad1378)\
**Post date:** [December 31, 2022, 7:03pm UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289/4 "2022-12-31T19:03:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![SaJjad1378](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaJjad1378](https://forums.unrealengine.com/u/SaJjad1378)\
**Post date:** [December 31, 2022, 7:05pm UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289/5 "2022-12-31T19:05:27Z")

</div>

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

```auto
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)
					);
			}
		}

```

---

<div class="post-metadata">

**Author:** ![SaJjad1378](https://d3kjluh73b9h9o.cloudfront.net/original/4X/2/8/5/28502927f1d97ba1051b3b47ccfe17233bf41ef0.png) [@SaJjad1378](https://forums.unrealengine.com/u/SaJjad1378)\
**Post date:** [December 31, 2022, 7:40pm UTC](https://forums.unrealengine.com/t/the-game-cracshes-on-joinsession-function/739289/6 "2022-12-31T19:40:08Z")

</div>

I don’t know have but I fix it 😄  
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
