ARKit keeps on crashing

Ok, I found the crash point, so this can be considered as an answer :slight_smile:

A call to GetXRCamera needs to be done before this function is executed!
GetXRCamera initializes the weak pointer XRCamera to be not-null.


**XRCamera being null in FAppleARKitSystem::SetupCameraTextures was the crash point.**
void FAppleARKitSystem::SetupCameraTextures()
{
#if SUPPORTS_ARKIT_1_0
	if (CameraImage == nullptr)
	{
		CameraImage = NewObject<UAppleARKitTextureCameraImage>();
		CameraImage->Init(FPlatformTime::Seconds(), nullptr);
		FAppleARKitXRCamera* Camera = (FAppleARKitXRCamera*)XRCamera.Get(); // The fix is to call GetXRCamera().Get();
		Camera->SetOverlayTexture(CameraImage); // here in our case, Camera is nullptr
	}
	if (CameraDepth == nullptr)
	{
		CameraDepth = NewObject<UAppleARKitTextureCameraDepth>();
	}
#endif
}

The fix is to call this code before calling StartARFunction from blueprint:


Of course the engine should avoid accessing XRCamera pointer and use instead GetXRCamera() that makes sure the pointer is not null!

    #include "Engine/Engine.h"
    #include "Runtime/HeadMountedDisplay/Public/IXRTrackingSystem.h"
    
    #include "MyFunctionLibrary.generated.h"

        UFUNCTION(BlueprintCallable, Category = "MyCat")
        	static void Test1()
        	{
        		auto XRCamera = GEngine->XRSystem.IsValid() ? GEngine->XRSystem->GetXRCamera() : nullptr;
        		if (XRCamera.IsValid())
        		{
        			//conoutf('I', "XR camera is valid!");
        		}
        		//else
        			//conoutf('E', "XR camera is not valid!");
        	}