Access violation crash when dereferencing

Hello!
I’m a bit clueless why the game crashes when this code is executed. HitComp is valid and not null, so what is it?

APropPushable::APropPushable(const FObjectInitializer& ObjectInitializer)
    : Super(ObjectInitializer)
{
    Collision = CreateDefaultSubobject<UBoxComponent>(TEXT("BoxCollision"));
    Collision->OnComponentHit.AddDynamic(this, &APropPushable::OnHit);
}
 
void APropPushable::OnHit(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, FVector NormalImpulse, const FHitResult& Hit)
{
    if (HitComp != nullptr)
    {
        UE_LOG(LogTemp, Warning, TEXT("NOT NULL"))
        if (IsValid(HitComp))
        {
            UE_LOG(LogTemp, Warning, TEXT("VALID"))
            FVector test = HitComp->GetCenterOfMass();     //<-- CRASH
        }
    }
}

Do you get any error message?

yes, the thread is trying to access a virtual address it has no access to

Did you try casting HitComp to a UBoxComponent? Just a shot in the dark.

Another hit in the dark… You could do this additional check…
IsPendingKill() on the actor.

No luck :frowning: This is what I did:

    	if (HitComp != nullptr)
    	{
    		UE_LOG(LogTemp, Warning, TEXT("NOT NULL"))
    		if (IsValid(HitComp))
    		{
    			UE_LOG(LogTemp, Warning, TEXT("VALID"))
    			if (!(OtherActor->IsPendingKill()))
    			{
    				UBoxComponent* box = Cast<UBoxComponent>(HitComp);
    				FVector test = box->GetCenterOfMass();     //<-- CRASH
    			}
    		}
    	}

Is there any way to set a breakpoint so I can see the value of the HitComp? Or is there anything else I can do to find out what is up with that variable?

Edit: I opened the crashdump and I was able to fetch this:

+HitComp0x0000021f12e3e400 {MinDrawDistance=??? LDMaxDrawDistance=??? CachedMaxDrawDistance=??? ...}UPrimitiveComponent *

So it actually is a primitive component, that explains why all the checks didn’t help. All the value seem to be ??? though.

Look at this: UPrimitiveComponent::GetCenterOfMass | Unreal Engine Documentation

Try putting “None” as the argument for GetCenterOfMass.