Access Violations using Getter Functions in a Subclass of ActorComponent

I am attempting to create an advanced movement component which extends from UCharacterMovementComponent that includes more options than just the default character movement. Upon calling the constructor when I create a subobject, the class uses a few getter functions, such as GetCharacterOwner()->GetWorld()->GetGravityZ(). When running the debugger to enter the editor, it calls __debugbreak() (unhandled exception) to cancel opening the editor, and says there’s an access violation. The memory address doesn’t return all zeros so it cannot be nullptr I believe (yet it is a low value).
Screenshot 2022-12-28 175353

Here’s the code snippet:

// constructor
UAdvancedMovementComponent::UAdvancedMovementComponent(const FObjectInitializer& foi) : Super(foi)
{
	PrimaryComponentTick.bCanEverTick = true; // Tick every frame

	gravDefault = GetCharacterOwner()->GetWorld()->GetGravityZ(); // Initial Gravity ( THE EXCEPTION IS THROWN HERE )
	FTimerHandle handle;
	GetCharacterOwner()->GetWorld()->GetTimerManager().SetTimer(handle, this, &UAdvancedMovementComponent::wallrunTick, 0.02f, true); // Start loop-checking for wallrunning
}

I will say I am new to using Unreal Engine, but please share your thoughts if you have any.

Thanks!

In your Constructor, the component has no Owner, and is not part of any World, so all of that is going to fail.

BeginPlay is the function you should use for most any sort of setup that has a dependency on something in the world

and if you look at the other spots in the debugger, with the call stack, and the variables, you should quickly see that GetCharacterOwner() is returning null.

1 Like