Editor crash due to thread problem

Hello,

Every time I try to run the game I’m having a problem with a function, everything work correctly If I comment the two line who assign the variable “NewController”.

the function in question :

"//Start

FHitResult HitResult;

FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
FVector EndTrace = (ForwardVector * 300.0f) + StartTrace;

FCollisionQueryParams QueryParams;
QueryParams.AddIgnoredActor(this);

ACtrl_GamplayController* NewController = Cast<ACtrl_GamplayController>(GetController());

if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, ECC_Visibility, QueryParams) && Controller) {
	
	if (AUsable* Usable = Cast<AUsable>(HitResult.GetActor())) {
		NewController->CurrentUsable = Usable; // <= THIS CRASH
		return;
	}

}

NewController->CurrentUsable = nullptr; // THIS CRASH"

The declaration of “CurrentUsable” in Ctrl_GamplayController.h :

“//
UPROPERTY(BlueprintReadWrite, VisibleAnywhere)
class AUsable* CurrentUsable;
//”

The problem comes from the two line "NewController->CurrentUsable = XXX ; "
From start CurrentUsable is NULL, but why does the program crash when I try to assign it a valule?

Dump file :
The thread tried to read from or write to a virtual address for which it does not have the appropriate access
Exception code: 0xC0000005

I’m new to C++ but it look like a Nullpointer exception I would understand why it’s crashing if I tried to get the variable but why does the set variable crash?

I don’t find any way to solve this issue so if someone have an idea you’re more than welcome. ^^

You’re crashing because NewController is likely null.

Add in a check for that and see what you get.

 FHitResult HitResult;
 
 FVector StartTrace = FirstPersonCameraComponent->GetComponentLocation();
 FVector ForwardVector = FirstPersonCameraComponent->GetForwardVector();
 FVector EndTrace = (ForwardVector * 300.0f) + StartTrace;
 
 FCollisionQueryParams QueryParams;
 QueryParams.AddIgnoredActor(this);
 
 ACtrl_GamplayController* NewController = Cast<ACtrl_GamplayController>(GetController());
 
if (NewController) // Make sure the controller is valid.
{

 if (GetWorld()->LineTraceSingleByChannel(HitResult, StartTrace, EndTrace, ECC_Visibility, QueryParams) && Controller) {
     
     if (AUsable* Usable = Cast<AUsable>(HitResult.GetActor())) {
         NewController->CurrentUsable = Usable; 
         return;
     }
 
 }
 
 NewController->CurrentUsable = nullptr;

}

Thank you, in fact it’s the NewController who’s null, but why the cast isn’t working? The class ACtrl_GameplayController inherit from APlayerController.

I also tried it with UGamplayStatics::GetPlayerController(this, 0)

This is getting wierder… I just debug, and it tell me that NewController is not null. I’ll try to initialize it out side the method to see if it’s working.

It’s exactly what I did ^^ check the value of NewController before and after the cast, and from what I see on the debuger the cast worked well.

That I can’t tell you without seeing more of your code. Put a break point where you are getting the controller and see what type of controller is is returning.AController* MyController = GetController(); // Put a breakpoint here and see what MyController is.

I wouldn’t expect GetController to be null. What’s failing is likely the Cast which then returns null. You’ll need to track down what type of controller is being returned and why its not what you expect it to be.

I manage to get it work… I definitly need to sleep ^^ I just pass 4hours trying to debug this function, but I forgot to change the game controller in the game mode… So my function worked correctly since the begining it’s just me who forgot to change the controller ^^ Thanks for the answer anyway :slight_smile: