Widget Initialization

Hello, I’m an Unreal Engine Beginner and I have a problem in Widgets initialization. When i’m trying to Create a new instance with CreateWidget function it gives me an EXCEPTION_ACCESS_VIOLATION error and I’m not able to find a way to solve this issue.
This is my Code:

void AMyHUD::CreateMenu()
{
	if (!exsist) {
		if (PlayerOwner && MenuAsset) {
			MainMenu = CreateWidget<UMenu>(PlayerOwner, MenuAsset);

			MainMenu->SetModifiers();
			exsist = true;

		}
	}
}

and here MyHUD.h

UCLASS()
class IMPRESSIONISMO_API AMyHUD : public AHUD
{
	GENERATED_BODY()

	bool exsist = false;

public:
	
	UPROPERTY(EditAnywhere)
		TSubclassOf<class UMenu> MenuAsset;

	class UMenu* MainMenu=nullptr;

	void CreateMenu();
	void DisplayMenu();
	void DestroyMenu();
};

UMenu is a subclass of UUserWidget that I have created in c++. UMenu is a parent class of the blueprint class stored into MenuAsset.
Thanks for the help.

Hi!
You need to add UPROPERTY macro for your pointer variables.

UPROPERTY()
UMenu* MainMenu = nullptr;

EXCEPTION_ACCESS_VIOLATION means that you are trying to access null pointer.
GarbageCollectors keep track of every created UObject. If you don’t have any pointer with the address of this UObject then the garbage collector will destroy it to free the memory. But garbage collector knows only about pointers that are marked with UPROPERTY macro.
Also, you should always check your pointers before using them.

There are some links that could be useful:

Another possibility is that your AMyHUD pointer is null.
Breaking on that access violation in the debugger, and looking at the various variable values, is an important step in debugging!

Hi,
I tried to add UPROPERTY macro in the variable declaration but it doesn’t work.
I have read the pages that you have linked and i found it very useful to understand unreal’s basics.
Thanks a lot for the help.

Hi,
You are right the AMyHUD pointer was null. To fix this issue I have simply deleted the AMyHUD class and make all the stuffs in the PlayerController class. Why did Unreal not create an object for the default HUD class? I setted all the default classes in the GameMode constructor like this:

static ConstructorHelpers::FClassFinder<APawn> PlayerPawnBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/ThirdPersonCharacter"));
	static ConstructorHelpers::FClassFinder<AMyPlayerController> PlayerControllerBPClass(TEXT("/Game/ThirdPersonCPP/Blueprints/BP_PlayerController"));
	static ConstructorHelpers::FClassFinder<AMyPlayerController> HUDAsset(TEXT("/Game/ThirdPersonCPP/Blueprints/BP_MyHUD"));
	if (PlayerPawnBPClass.Class != NULL && PlayerControllerBPClass.Class != NULL && HUDAsset!=NULL)
	{
		HUDClass = HUDAsset.Class;
		DefaultPawnClass = PlayerPawnBPClass.Class;
		PlayerControllerClass = PlayerControllerBPClass.Class;
		
	}

and the only object that wasn’t initialized correctly was the HUD. Why this behaviour?
The HUD class loaded is a Blueprint subclass of AMyHUD.
Thanks a lot for the help.

Why did Unreal not create an object for the default HUD class?

Are you sure it didn’t? Where do you assign a value to your own variable named AMyHUD? That doesn’t happen automatically.

Hi,
I haven’t created a AMyHUD Object “manually” but I have setted the HUD class in the gamemode like the playercontroller and the playerpawn.
For example, also for the playercontroller I haven’t created an object “manually”, I have only setted the defalut class on the gamemode, but, if i get the playercontroller, it doesn’t return NULL, but if i try to get the HUD it returns NULL.
I have declared both in the same way, so, Why is the HUD NULL and the playercontroller not?

First thing when checking validity of pointers, I highly recommend getting into the habit of using IsValid(pointer), it’s the same as checking pointer != nullptr but it also checks pointer->IsPendingKill().

I don’t believe AHud automatically sets PlayerOwner, as I ran into that issue, here’s my code:

//AHUD_Player& AHUD_Player::Initialize(TObjectPtr<ACharacter_Player> inPlayer)//UE5 standard only
AHUD_Player& AHUD_Player::Initialize(ACharacter_Player* inPlayer)
{
	player = inPlayer;
	PlayerOwner = player->GetController<APlayerController>();
	
	rolesMenu = CreateWidget<URolesMenu>(GetOwningPlayerController(), Retrieval::GetRolesMenuBP(*player), "RolesMenu");
	if (IsValid(rolesMenu)) 
	{ 
		rolesMenu->AddToViewport(9);//9 so that it is highest z-order
		rolesMenu->playerHud = this;
	}
	else { Global::Log(EHierarchyVerbosity::DEBUG, *this, "Initialize", TEXT("Roles Menu was not created")); }
}

Edit: Seeing your last post, you do have to create the AHud. If you saw that the error mentioned something other than AHud, sometimes accessing a nullptr via pointer->somemethodorproperty won’t actually tell you the pointer is what’s null. I’ve had cases where it reached execution of the function and the exception was thrown within that function, without saying it’s actually the pointer.

		FActorSpawnParameters SpawnInfo;
		SpawnInfo.Owner = this;//This is an ACharacter representing my player
		SpawnInfo.Instigator = GetInstigator();
		SpawnInfo.ObjectFlags |= RF_Transient;	// We never want to save HUDs into a map
		SpawnInfo.SpawnCollisionHandlingOverride = ESpawnActorCollisionHandlingMethod::AlwaysSpawn;
		playerHud = GetWorld()->SpawnActor<AHUD_Player>(SpawnInfo);
		GetController<APlayerController>()->MyHUD = playerHud;

		playerHud->Initialize(this);