Random Access Violation crashing

Hello everyone. I have a test function on a UObject script that gets called when I press LMB from another script. When I spam click it the game crashes with this:

Exception thrown at 0x000000020000005D in UnrealEditor-Win64-DebugGame.exe: 0xC0000005: Access violation executing location 0x000000020000005D.

Unhandled exception at 0x000000020000005D in UnrealEditor-Win64-DebugGame.exe: 0xC0000005: Access violation executing location 0x000000020000005D.

This happens every time, but I have to spam fire button for about 1 minute for it to crash.

{
    FHitResult hitInfo;

    if (!PlayerCamera)
    {
        GEngine->AddOnScreenDebugMessage(-1, 35.0, FColor::Red, "Null Ptr Camera");
        return;
    }

    FVector EndVector = PlayerCamera->GetCameraLocation() + PlayerCamera->GetActorForwardVector() * 900;

    bool hitBool = GetWorld()->LineTraceSingleByObjectType(hitInfo, PlayerCamera->GetCameraLocation(), EndVector, ECollisionChannel::ECC_PhysicsBody);

    if (hitBool) {
        FVector hitPoint = hitInfo.ImpactPoint;
        AActor* MyActor = hitInfo.GetActor();

        if (MyActor)
        {

            UStaticMeshComponent* Mesh = Cast<UStaticMeshComponent>(MyActor->GetRootComponent());
            if (Mesh)
            {
                Mesh->AddImpulseAtLocation(PlayerCamera->GetActorForwardVector() * 1500, hitPoint);
            }
            else
            {
                GEngine->AddOnScreenDebugMessage(-1, 35.0, FColor::Red, "Null Ptr mesh");
            }
        }
        else
        {
            GEngine->AddOnScreenDebugMessage(-1, 35.0, FColor::Red, "Null Ptr actor");
        }

    }
}

Also VS adds breakpoint symbol to bool hitBool = GetWorld()->LineTraceSingleByObjectType(hitInfo, PlayerCamera->GetCameraLocation(), EndVector, ECollisionChannel::ECC_PhysicsBody); line after the crash.

Probably post the stack trace or run it with the debugger, it is possible GetWorld() returns null and you are using it without a check

I’ve tried creating UWorld variable for the world and now the program crashes after I use GetWorld(). Is there something wrong with using GetWorld() inside an UObject?
image

I believe it is not fully implemented in UObject the same way as it is in AActor for example, you can use GEngine->GetWorld() for a more global method

This has fixed the crash but now I see that it always returns null pointer

Seems there are some extra considerations when using GetWorld() inside a UObject:

1 Like

Thank you. I’ve decided to just add an AActor variable to the script and get world through it and it works.

1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.