Download

unreal engine crashing when the BeginOverlapEvent gets called

I have 3 weapons placed near each other and I’m showing a simple widget unique to each weapon whenever my character enters their sphere component and it works but whenever I make my character run in between them for like a minute switching between widgets unreal crashes. I commented out my end overlap handler and it crashed again but when I commented out my begin overlap handler there was no crashing. Also by using show Collisions I made sure that it crashes whenever a BeginOverlapEvent is fired.
BeginOverlaphandler:


    UE_LOG(LogTemp, Warning, TEXT("Started"));
    ATPSShooterCharacter* MyCharacter = Cast<ATPSShooterCharacter>(OtherActor);
    if (MyCharacter != nullptr) {
        UE_LOG(LogTemp, Warning, TEXT("Cast Success - Chr:%p - Wep:%p"), MyCharacter, this);
        AMasterWeapon* TempW = MyCharacter->CurrentOverlappedWeapon;
        MyCharacter->CurrentOverlappedWeapon = this;
        UE_LOG(LogTemp, Warning, TEXT("Changed Weapon - Prev:%p - Cur:%p"), TempW, MyCharacter->CurrentOverlappedWeapon);
        if (TempW != nullptr) {
            TempW->DisableInput(Cast<APlayerController>(MyCharacter->GetController()));
            TempW->POwner = nullptr;
            if (TempW->WidgetInstance->IsInViewport()) {
                TempW->WidgetInstance->RemoveFromViewport();
            }
            UE_LOG(LogTemp, Warning, TEXT("Disabled Input on %p"), TempW);
        }
        POwner = MyCharacter;
        EnableInput(Cast<APlayerController>(MyCharacter->GetController()));
        if (!WidgetInstance->IsInViewport()) {
            WidgetInstance->AddToViewport();
        }
        UE_LOG(LogTemp, Log, TEXT("Enabled Input on %p"), this);
    }

Also I’m not getting any logs before it crashes. What am I doing wrong?

Never used %p before, so I can’t tell if that is it or not.
With that in mind, try replacing all the %p’s with %s and wrap the pointers in *GetNameSafe(<the pointer>) instead.

e.g.


 
 UE_LOG(LogTemp, Warning, TEXT("Changed Weapon - Prev:%s - Cur:%s"), *GetNameSafe(TempW), *GetNameSafe(MyCharacter->CurrentOverlappedWeapon))


The logs were there for me to see which section of the code was causing the crash and they were added later. None of them gets outputted when it crashes (when it doesn’t they do get printed correctly) which I assume means that when the crash occurs my function hasn’t been even called yet.

Groovy.

Don’t recall seeing %p in use for actors is all :slight_smile:

Is WidgetInstance is alway valid?

Is something being garbage collected?
i.e. CurrentOverlappedWeapon is not a UPROPERTY()

Using UPROPERTY() for CurrentOverlappedWeapon fixed it, thanks a lot.

Thanks for the follow up.

Remember people - Only you can prevent Garbage Collection!