title: UE4 C++ Access violation on class variable and unable to read memory on watchpoint
body: <p>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 <code>&lt;Unable to read memory&gt;</code>. 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!!</p>
  <pre><code>private:
      /** Crosshair asset pointer */
      class UTexture2D* CrosshairTex;
      UHUDWidget* HUDWidget;
  
  void  ATheLostMine2HUD::BeginPlay()
  {
      Super::BeginPlay();
  
      if (HUDWidgetClass)
      {
          HUDWidget = CreateWidget&lt;UHUDWidget&gt;(GetWorld(), HUDWidgetClass);
  
          /** Make sure widget was created */
          if (HUDWidget)
          {
              /** Add it to the viewport */
              HUDWidget-&gt;AddToViewport();
          }
      }
  }
  
  void ATheLostMine2HUD::Tick(float DeltaSeconds)
  {
      Super::Tick(DeltaSeconds);
      if (HUDWidget)
      {
          if (GEngine)
              GEngine-&gt;AddOnScreenDebugMessage(-1, 15.0f, FColor::Yellow, FString::Printf(TEXT(&quot;Have HUDWidget&quot;)));
  
      }
  }
  
  void ATheLostMine2HUD::UpdateCompassPointer(float Yaw, float Degrees, float Direction, float Quad)
  {
      if (HUDWidget)
      {
          HUDWidget-&gt;UpdateCompassPointer(Yaw, Degrees, Direction, Quad);
      }
      else
          UE_LOG(LogTemp, Warning, TEXT(&quot;HUDWidget is not instantiated&quot;));
  }
  </code></pre>
  <p>More info and a question. Thanks for the nice comments, everybody! So I know I'm setting HUDWidget correctly because it works fine if I put it in my code. So, somehow, it's losing the value over in the header and the debugger is stopping on the If instead of the statement that caused it (as Ruzihm suggested). I'll put the UPROPERTY in as suggested and see what that does. Why would it get stale? I have another game with this exact same code (almost) and it works with no problems... I'll report back after the UPROPERTY update but I'm getting used to it being at the top of my code...
  Curiouser and curiouser... the UPROPERTY didn't help anything.  So, in the Tick code I inserted a call using the HUDWidget pointer so it would crap out instead of the other function. It's still doing the same thing. The Tick code works and the other one bytes the dust!! I don't have a clue!!  Yeah, I figured it wasn't going to be something obvious. I don't need help with it. I can cut it down and build it back up myself and figure it out if I feel like it. It's a strange one, though. I appreciate the effort and offer, though! Thanks!</p>