EXCEPTION_ACCESS_VIOLATION when CaptureScene()

Hello, I’m still not that acquainted with unreal but I’m trying to implement a portal system, doing it by this tutorial: Creating seamless Portals in Unreal Engine 4. I am almost done with it, the only thing that is left is to fix culling problem, but I wanted to test if portals actually work b4 that. The problem that I am facing is that UE crashes with this error:

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x00000000

UE4Editor_Renderer
UE4Editor_Renderer
UE4Editor_Engine
UE4Editor_UnusualTripGame_0008!APortalManager::UpdateCapture() [E:\GitHub\UnusualTrip\Source\UnusualTripGame\GameplayMechanics\Portal\PortalManager\PortalManager.cpp:215]
UE4Editor_UnusualTripGame_0008!AMainCharacter::TickActor() [E:\GitHub\UnusualTrip\Source\UnusualTripGame\MainCharacter\MainCharacter.cpp:68]
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Core
UE4Editor_Core
UE4Editor_Core
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine
UE4Editor_Engine

The TickActor() in MainCharacter.cpp:

void AMainCharacter::TickActor(float DeltaTime, ELevelTick TickType, FActorTickFunction& ThisTickFunction)
{
	Super::TickActor(DeltaTime, TickType, ThisTickFunction);

	APlayerController* PlayerController = UGameplayStatics::GetPlayerController(GetWorld(), 0);

	if (PlayerController)
	{
		AMainCharacterPlayerController* MainCharacterPlayerController = Cast<AMainCharacterPlayerController>(
			PlayerController);
		if (MainCharacterPlayerController)
		{
			if (MainCharacterPlayerController->PortalManager)
			{
				MainCharacterPlayerController->PortalManager->Update(DeltaTime);
			}
		}
	}
} //68th line

The Update() function in PortalManager.cpp:

void APortalManager::Update(float DeltaTime)
{
//Generate Portal texture
UpdateDelay += DeltaTime;

if (UpdateDelay > 1.f)
{
	UpdateDelay = 0.f;
	GeneratePortalTexture();
}

//Find portals in the level and update them
APortal* Portal = UpdatePortalsInWorld();

if (Portal) UpdateCapture(Portal); //function call that presumably crashes the editor

}

And finally UpdateCapture() fucntion in PortalManager.cpp:

void APortalManager::UpdateCapture(APortal* Portal)
{
	if (!ControllerOwner) return;

	ACharacter* Character = ControllerOwner->GetCharacter();
	AMainCharacter* MainCharacter = nullptr;
	if (Character)
	{
		MainCharacter = Cast<AMainCharacter>(Character);
		if (!MainCharacter) return;
	}
	else return;

	//Update SceneCapture (discard if there is no active portal)
	if (SceneCapture && PortalTexture && Portal)
	{
		UCameraComponent* PlayerCamera = MainCharacter->GetCameraComponent();
		AActor* Target = Portal->GetTarget();

		//Place the SceneCapture to Target
		if (Target && PlayerCamera)
		{
			//Compute new location in space of the target actor
			//(which may nor be aligned to world)
			FVector NewLocation = UTool::ConvertLocationToActorSpace(
				PlayerCamera->GetComponentLocation(),
				Portal,
				Target
			);
			SceneCapture->SetWorldLocation(NewLocation);

			//Compute new Rotation in the space of the
			//Target location
			FTransform CameraTransform = PlayerCamera->GetComponentTransform();
			FTransform SourceTransform = Portal->GetActorTransform();
			FTransform TargetTransform = Target->GetActorTransform();

			FQuat LocalQuat = SourceTransform.GetRotation().Inverse() * CameraTransform.GetRotation();
			FQuat NewWorldQuat = TargetTransform.GetRotation() * LocalQuat;

			//Update SceneCapture rotation
			SceneCapture->SetWorldRotation(NewWorldQuat);

			//Clip Plane : to ignore object between the
			//SceneCapture and the Target of the portal
			SceneCapture->ClipPlaneNormal = Target->GetActorForwardVector();
			SceneCapture->ClipPlaneBase = Target->GetActorLocation() + 
				(SceneCapture->ClipPlaneNormal * -1.5f); //Offset to avoid visible pixel border
		}

		//Switch on the valid Portal
		Portal->SetActive(true);

		//Assign the Render Target
		Portal->SetRTT(PortalTexture);
		SceneCapture->TextureTarget = PortalTexture;

		//Get the Projection Matrix
		SceneCapture->CustomProjectionMatrix = ControllerOwner->GetCameraProjectionMatrix();

		SceneCapture->CaptureScene(); //215th line, the one from crash error msg
	}
}

I presume that CaptureScene() somehow crashes the editor but I have no idea why, since I do nullptr check for everything and SceneCapture is not nullptr either.

If any other code needs to be provided, please tell! Thank you!

I had the same issues, and managed to figure out why it was happening!

In my case, my character (“AMainCharacter” in your case) extended ACharacter, not ACameraCharacter.
This means that at runtime character->GetCameraComponent() will not exist, as that function is from ACameraCharacter.

Either let your character extend ACameraCharacter, or implement your own GetCamera equivalent.

I soved it by adding

UFUNCTION(BlueprintNativeEvent, BlueprintCallable, Category = "RemoteCaptureWindow|WindowCapture")
		UCameraComponent* GetCamera();

To my character class, and in blueprint just overriding it and providing the blueprints camera component.

You can also simply your code a bit

ACharacter* Character = ControllerOwner->GetCharacter();
     AMainCharacter* MainCharacter = nullptr;
     if (Character)
     {
         MainCharacter = Cast<AMainCharacter>(Character);
         if (!MainCharacter) return;
     }

Can just be

MainCharacter * Character = Cast<MainCharacter>(ControllerOwner->GetCharacter());