Build Error - Unhandled Exception: EXCEPTION_ACCESS_VIOLATION 0x00007fff013de428

Hi all, I created a Character class and use it for my game. For some reason I get the above error when building and it crashes unreal. I believe it has something to do with the line of code here: GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ATutorialCharacter::OnBeginOverlap);

When I comment this out, compile build then put it back in it seems to work fine for some reason. I could keep doing that but I would like to know how to fix this. I also cant seem to get my settings to save but thats for a different post I think! lol

Hi!

Maybe it has something to do with ATutorialCharacter::OnBeginOverlap signature.
Are you sure it’s right? It must correspond to delegate OnComponentBeginOverlap and look something like this:

void ATutorialCharacter::OnBeginOverlap(
UPrimitiveComponent* OverlappedComponent, 
AActor* OtherActor, 
UPrimitiveComponent* OtherComp, 
int32 OtherBodyIndex, 
bool bFromSweep, 
const FHitResult &SweepResult);

Or can you, please, provide your code?

Sure, here is the Begin play function which is where the code that is causing the error is.
void ATutorialCharacter::BeginPlay()
{
Super::BeginPlay();
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ATutorialCharacter::OnBeginOverlap);
if (Player_Power_Widget_Class != nullptr) {
Player_Power_Widget = CreateWidget(GetWorld(), Player_Power_Widget_Class);
Player_Power_Widget->AddToViewport();
}
}

And here is the OnBeginOverlap Function:

void ATutorialCharacter::OnBeginOverlap(UPrimitiveComponent* HitComp, AActor* OtherActor, UPrimitiveComponent* otherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& SweepResult)
{
if (OtherActor->ActorHasTag(“CapyPickUp”)) {
UE_LOG(LogTemp, Warning, TEXT(“COLLIded with”));

	Power += 10.0f;
	if (Power > 90) {
		UE_LOG(LogTemp, Warning, TEXT("Power Increased"));
	}
	
	//FVector Transform = this->GetActorLocation();
	//Transform.Z += 100.0f;
	OtherActor->Destroy();
	/*FName SocketName = FName("CapySocket"); // The name of your socket
	EAttachmentRule LocationRule = EAttachmentRule::SnapToTarget; // How to handle location when attaching
	EAttachmentRule RotationRule = EAttachmentRule::KeepRelative; // How to handle rotation when attaching
	EAttachmentRule ScaleRule = EAttachmentRule::KeepRelative; // How to handle scale when attaching
	bool bWeldSimulatedBodies = false;
	OtherActor->AttachToComponent(GetMesh(), FAttachmentTransformRules(LocationRule, RotationRule, ScaleRule, bWeldSimulatedBodies), SocketName);
	*/
	//OtherActor->AttachToActor(this, rules);
}

}

UFUNCTION()
void ATutorialCharacter::OnBeginOverlap(…)

I always forget that too :slight_smile:

Does this mean you have to put UFUNCTION in front of the header file for OnBeginOverlap??!!

Yes

I actually have it in my header file already as shown here,

UFUNCTION()
void OnBeginOverlap(class UPrimitiveComponent* HitComp, class AActor* OtherActor, class UPrimitiveComponent* otherComp, int32 OtherBodyIndex,
bool bFromSweep, const FHitResult& SweepResult);

Do I need to put it in the cpp file as well then?

no, only in the .h file

check all your pointers… one of them must be NULL for sure

I do not see how they could be null, this error is fixed when I compile with
GetCapsuleComponent()->OnComponentBeginOverlap.AddDynamic(this, &ATutorialCharacter::OnBeginOverlap);
commented out, and then uncomment it out and recompile with no changes to anything in the scene.

this:

GetCapsuleComponent()

or this:

&ATutorialCharacter::OnBeginOverlap

or inside this

ATutorialCharacter::OnBeginOverlap()

IsValid(GetCapsuleComponent())
IsValid(OtherActor)
IsValid(GetMesh())

UFUNCTION()
void OnBeginOverlap();//–>Change this name (in case…)


UPDATE

Are you destroying an actor?

OtherActor->Destroy();

And then you attach it?

OtherActor->AttachToActor(this, rules); //–> BOOM

as for GetCapsuleComponent() I do not pass any parameters in it so there cannot be pointers. As for ATutorialCharacter::OnBeginOverlap() and ATutorialCharacter::OnBeginOverlap() I do not see how any of these could be null as I do not pass any parameters into these either. I think it is fulfilled by the engine but I’m a beginner so I am not sure of that. Even if one of these were null, how would that change after recompiling with it commented out?

it is a pointer (return a pointer)

I dont think that it is null because I call GetCapsuleComponent() in the constructor for the class here GetCapsuleComponent()->InitCapsuleSize(42.0f, 96.0f); which I believe gets called first.

if you don’t check ALL your pointers you will have this problem always

If the problem is indeed that there is a pointer that is null, how could it be that the pointers are no longer null after compiling without that line of code in the .cpp file? Nothing is changed between then so the pointers should not be changer or am I missing something?

Unreal works in a very strange way…
I do not have all the answers.
But I do know one thing.
Exception Access Violation is the fault of null pointers.
And i will tell you more.
99% of problems in C++ are caused by pointers.

Thank you for this, how do you suggest I check for null pointers? I’m not exactly sure how to check if parameters/variables I did not create are null or not.

Pointers are always declared with an asterisk.
Pointers members are always accessed using an arrow →
Pointers of UObject are checked using IsValid() function
UObject pointers must be declared with UPROPERTY()
Delegate functions in unreal must be declared using UFUNTION()
raw pointers in C++ are checked with nullptr