Memory access violation issue

Hi again everyone.

I am greceiving error:

"Exception thrown: read access violation.

this was 0x4C0.

If there is a handler for this exception, the program may be safely continued."

OR

Unhandled exception at 0x00007FFEB93B3A11 (UE4Editor-OCOL-Win64-DebugGame.dll) in UE4Editor.exe: 0xC0000005: Access violation reading location 0x0000000000000248.

===

I know that I am close on solving this one but am having some trouble. It looks like it is trying to utilize uninitialed memory but I can not see where this is happening.
I tried stepping into the code but did not make progress. I know it says that I can make an exception handler above however I am not a fan of doing that. I prefer
finding the problem and solving it.

Essentially what I am trying to do is add a triggerbox to my actor class. I have not added a mesh or anything yet to the code. Here is what I have:

===
Header:



#pragma once

#include "GameFramework/Actor.h"
#include "OLTest.generated.h"

UCLASS()
class OCOL_API AOLTest : public AActor
{
	GENERATED_BODY()
	
public:	

	AOLTest();

	//This is for the triggerBox
	UPROPERTY(BlueprintReadWrite, Category = "TriggerBox")
		UBoxComponent* triggerBox;

	UPROPERTY(BlueprintAssignable, Category = "Collision")
		FComponentBeginOverlapSignature OnComponentBeginOverlap;

	UFUNCTION()
		void TriggerEnter(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const 

FHitResult& SweepResult);


};


CPP file:



#include "OCOL.h"
#include "OLTest.h"


// Sets default values
AOLTest::AOLTest()
{

	triggerBox->bGenerateOverlapEvents = true;

	triggerBox->OnComponentBeginOverlap.AddDynamic(this, &AOLTest::TriggerEnter);
	triggerBox->bGenerateOverlapEvents = true;
	triggerBox->SetRelativeLocationAndRotation(FVector(0.0f, 0.0f, 0.0f), FRotator(0.0f, 0.0f, 0.0f));
	triggerBox->SetRelativeScale3D(FVector(15.0f, 15.0f, 5.0f));
	RootComponent = triggerBox;

}
//FComponentBeginOverlapSignature
void AOLTest::TriggerEnter(UPrimitiveComponent* OverlappedComp, AActor* OtherActor, UPrimitiveComponent* OtherComp, int32 OtherBodyIndex, bool bFromSweep, const FHitResult& 

SweepResult)
{
	//Future code will go here.
}



===

Any thoughts?

Oh yes, I am on:

Windows 10 Pro 64 bit
Unreal Engine 4.12
Visual Studio Enterprise 2015
Version 14.025123.00 Update 2

I can post more info if needed.

You’re trying to dereference ‘TriggerBox’ in the constructor, but you haven’t created the object. That’s why you’re getting an access violation.

You need to use the FObjectInitializer constructor (make sure you call Super too), and use ObjectInitializer to create the trigger box, then you can set properties on it.

e.g.



AOLTest::AOlTest(const FObjectInitializer& ObjectInitializer) : Super(ObjectInitializer)
{
    TriggerBox = ObjectInitializer.CreateDefaultSubobject<UBoxComponent>(this, FName("TriggerBox"));
    //Do Stuff
}


2 Likes

You solved it :slight_smile:

I am awarding you the keys to the city and there is a parade being thrown in your honor.

How do I mark your answer as correct?

Can’t do that on the forums, everything is an open discussion :slight_smile: