UE4 C++ Access violation on class variable and unable to read memory on watchpoint

  access-violation, c++, unreal-engine4

I’ve been struggling with this problem for hours and I’ve looked and can’t find the answer so sorry if it’s been resolved elsewhere. I can’t find an answer that matches. I’m alone and need help. I allocated a pointer in my class in BeginPlay. I reference it on every tick and it’s fine. Then, when I call another event/function (UpdateCompassPointer) it blows up on the IF test to see if it’s null or not with a read access violation! The watchpoint says <Unable to read memory>. All I can figure is that I’ve stepped all over my class but I don’t see it. Here’s my definition and then the code. The variable is HUDWidget. It’s as fine as frog’s hair on the tick, though!!!! Thanks for any help!
Update: I put another variable at the top of my cpp module to hold the same pointer and am using that for that one function and it’s working fine. The Tick function continues to run with no problem but the other function blows up on any reference to that variable if I use it. AMAZING!!

private:
    /** Crosshair asset pointer */
    class UTexture2D* CrosshairTex;
    UHUDWidget* HUDWidget;

void  ATheLostMine2HUD::BeginPlay()
{
    Super::BeginPlay();

    if (HUDWidgetClass)
    {
        HUDWidget = CreateWidget<UHUDWidget>(GetWorld(), HUDWidgetClass);

        /** Make sure widget was created */
        if (HUDWidget)
        {
            /** Add it to the viewport */
            HUDWidget->AddToViewport();
        }
    }
}

void ATheLostMine2HUD::Tick(float DeltaSeconds)
{
    Super::Tick(DeltaSeconds);
    if (HUDWidget)
    {
        if (GEngine)
            GEngine->AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT("Have HUDWidget")));

    }
}

void ATheLostMine2HUD::UpdateCompassPointer(float Yaw, float Degrees, float Direction, float Quad)
{
    if (HUDWidget)
    {
        HUDWidget->UpdateCompassPointer(Yaw, Degrees, Direction, Quad);
    }
    else
        UE_LOG(LogTemp, Warning, TEXT("HUDWidget is not instantiated"));
}

Source: Windows Questions C++

LEAVE A COMMENT