TSubclassOf<UUserWidget> permanently crashing editor

This is my first time making a C++ Unreal project. I have been at it for the last few months. I have background in Unity C# as well as basic C++. I’ve been at this for weeks, and it is supposed to be my Senior Overview project, please bear with me.

I am using Unreal 5.0.3

At some point, I was trying to control widgets from my player class. During which I used a line in the header of something like this:
TSubclassOf mywidget;
UUserWidget * mywidgetinstance;

As well as some functions in the cpp file that included creating the widget and adding/removing from viewport. It threw some errors, I research, I add UMG to the .build file. Crashes the editor, editor will not open again. “Oh, last thing I did was add UMG to the build file, i will just undo that at least so the editor opens.” Does not work, I put it back in assuming that yes, I would eventually need the UMG module. More research. Find this answer in this thread. Doesn’t work. I comment out all code relating to the Widgets and re-build. Does not work. More research. At some point I install the debug symbols.

I remove the troublesome player script and rebuild…Unreal opens. Outside the project, I remove the widget code entirely, re add to project. Unreal opens. Okay. Add Widget lines back in. Crash. Remove widget lines, rebuild, crash. Maybe the project just broke. Migrate assets to new project (In attached code, this is why that is the project name, because I used it initially to make sure this wasn’t just a C++ UMG issue, then used it as a backup file). Everything works. Player script without hell UUserwidget code works. UMG being in the build file works. So maybe UUserwidget code will work. Crash. Oh, ok, but If I remove that UUserWidget code that I just added to the script that otherwise worked, it should work, right? No, crash, project won’t open.

Change what I am googling. Find out that maybe I shouldn’t be handling widgets in the player code from this thread. Sure, makes sense in hindsight. Remove Player script. Open engine. Set up according to that thread. All works. Add in player script with uuserwidget stuff commented. Doesn’t work. Wipe all mention of UUserwidget completely. Remove player script. Re Add to Project, rebuild. Okay. Everything works. For now. I do not know what I will do if it decides to turn against me again.

Below is the header and relevant functions after commenting the UUserWidget Stuff. Note that the errors that were given in the crash logs when no mention of UUserWidget exists always refers to around the line where the first mention of UUserWidget was, but otherwise the line works when I re-add it to the project. I know the rest of the code might look kind of ridiculous, but I honestly just want to figure out the issue of the UUserWidget stuff. Everything else works, and I would rather be left to find out the rest of my bad coding habits whilst actually finishing this project.

Header
#pragma once

#include "CoreMinimal.h"
#include "GameFramework/Character.h"
#include "GameFramework/CharacterMovementComponent.h"
#include "Components/SkeletalMeshComponent.h"
#include "Kismet/KismetMathLibrary.h"
#include "Components/SphereComponent.h"
#include "FumbleCharacter.generated.h"

//there is an enum here for movement 


UCLASS()
class CATSDODGEBALL_API AFumbleCharacter : public ACharacter
{
	GENERATED_BODY ( )


public:
	// Sets default values for this character's properties
	AFumbleCharacter();



	MoveMode movemodeenum { Walking };
	UCharacterMovementComponent * charmovement;
	USkeletalMeshComponent * charamesh;

	//Lots of Bools and floats you would expect from a player controller/
	

	//THESE ARE ALL FOR TERRESTRIAL

	//BALL

	class USphereComponent * PickupRange;
	AActor * ball;

	//THESE ARE FOR TRANSITIONS

	//this is for making the timers for ui bars work
	FTimerHandle bartimerhandle;
	

	bool jIsSuperJumping;
	bool jIsChargingSuperJump;
	float jSuperJumpHeight;
	float jSuperJumpHeld;
	

	//WIDGETS

//------UPROPERTY ( EditAnywhere, Category = "Super Jump" )
//-----------TSubclassOf<UUserWidget> SuperJumpWidget;
//------UUserWidget * SuperJumpWidgetInstance;

//all of my functions are below this. none take a widget-esque argument.
}
cpp file functions
// Sets default values
AFumbleCharacter::AFumbleCharacter()
{
 	// Set this character to call Tick() every frame.  You can turn this off to improve performance if you don't need it.
	PrimaryActorTick.bCanEverTick = true;
//------SuperJumpWidgetInstance = CreateWidget<UUserWidget> ( GetWorld ( ), SuperJumpWidget );
	charmovement = GetCharacterMovement ( );
	PickupRange = this->GetOwner ( )->FindComponentByClass<USphereComponent> ( );
	charamesh = this->GetOwner( )->FindComponentByClass<USkeletalMeshComponent> ( );
}

void AFumbleCharacter::jSuperJump ( )
{
	if ( movemodeenum == ChargeSuperJump )
	{
		//...there is code here but it is irrelevant
	}
	else if ( movemodeenum == Walking )
	{
		movemodeenum = ChargeSuperJump;
		jIsChargingSuperJump = true;
		tIsWalking = false;
		tCanWalk = false;

		//Make Bar Widget

//-------SuperJumpWidgetInstance->AddToViewport ( );

		//Start Timer
		GetWorld ( )->GetTimerManager ( ).SetTimer( bartimerhandle, jSuperJumpDelegate, 0.03f, true, 0.f );
	}
}

void AFumbleCharacter::jSuperJumpUIClose ( ) // this is called by timer to remove widget shortly after action is complete but not immediately
{
//-------SuperJumpWidgetInstance->RemoveFromViewport( );
}

These are the errors it gives me in the crash handler when there is no more UUserWidget in my player script.

Callstack

[2023.04.06-14.25.52:227][ 0]LogWindows: Error: === Critical error: ===
[2023.04.06-14.25.52:227][ 0]LogWindows: Error:
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: Fatal error!
[2023.04.06-14.25.52:227][ 0]LogWindows: Error:
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000000
[2023.04.06-14.25.52:227][ 0]LogWindows: Error:
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff8e3943d44 UnrealEditor-TestingCproject.dll!AFumbleCharacter::AFumbleCharacter() [P:\Game Projects\TestingCproject\Source\TestingCproject\FumbleCharacter.cpp:12]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff95a3ec44d UnrealEditor-CoreUObject.dll!UClass::CreateDefaultObject() [D:\build++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Class.cpp:3897]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff95a6c04dd UnrealEditor-CoreUObject.dll!UObjectLoadAllCompiledInDefaultProperties() [D:\build++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectBase.cpp:836]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff95a6a030b UnrealEditor-CoreUObject.dll!ProcessNewlyLoadedUObjects() [D:\build++UE5\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\UObjectBase.cpp:920]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff95a5582a5 UnrealEditor-CoreUObject.dll!TBaseStaticDelegateInstance<void __cdecl(FName,bool),FDefaultDelegateUserPolicy>::ExecuteIfSafe() [D:\build++UE5\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateInstancesImpl.h:731]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff956d1a5c2 UnrealEditor-Core.dll!TMulticastDelegate<void __cdecl(FName,bool),FDefaultDelegateUserPolicy>::Broadcast() [D:\build++UE5\Sync\Engine\Source\Runtime\Core\Public\Delegates\DelegateSignatureImpl.inl:967]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff956d3e937 UnrealEditor-Core.dll!FModuleManager::LoadModuleWithFailureReason() [D:\build++UE5\Sync\Engine\Source\Runtime\Core\Private\Modules\ModuleManager.cpp:558]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff968adaaf6 UnrealEditor-Projects.dll!FModuleDescriptor::LoadModulesForPhase() [D:\build++UE5\Sync\Engine\Source\Runtime\Projects\Private\ModuleDescriptor.cpp:690]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff968adae31 UnrealEditor-Projects.dll!FProjectManager::LoadModulesForProject() [D:\build++UE5\Sync\Engine\Source\Runtime\Projects\Private\ProjectManager.cpp:62]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f053cb UnrealEditor.exe!FEngineLoop::LoadStartupModules() [D:\build++UE5\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:4118]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f0866e UnrealEditor.exe!FEngineLoop::PreInitPostStartupScreen() [D:\build++UE5\Sync\Engine\Source\Runtime\Launch\Private\LaunchEngineLoop.cpp:3469]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f00bbd UnrealEditor.exe!GuardedMain() [D:\build++UE5\Sync\Engine\Source\Runtime\Launch\Private\Launch.cpp:137]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f00e8a UnrealEditor.exe!GuardedMainWrapper() [D:\build++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:147]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f03c4d UnrealEditor.exe!LaunchWindowsStartup() [D:\build++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:283]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f15564 UnrealEditor.exe!WinMain() [D:\build++UE5\Sync\Engine\Source\Runtime\Launch\Private\Windows\LaunchWindows.cpp:330]
[2023.04.06-14.25.52:227][ 0]LogWindows: Error: [Callstack] 0x00007ff618f18736 UnrealEditor.exe!__scrt_common_main_seh() [d:\a01_work\6\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:288]
[2023.04.06-14.25.52:228][ 0]LogWindows: Error: [Callstack] 0x00007ff97bec7614 KERNEL32.DLL!UnknownFunction []
[2023.04.06-14.25.52:228][ 0]LogWindows: Error: [Callstack] 0x00007ff97dac26a1 ntdll.dll!UnknownFunction []
[2023.04.06-14.25.52:228][ 0]LogWindows: Error:
[2023.04.06-14.25.52:307][ 0]LogExit: Executing StaticShutdownAfterError
[2023.04.06-14.25.52:308][ 0]LogWindows: FPlatformMisc::RequestExit(1)
[2023.04.06-14.25.52:308][ 0]LogWindows: FPlatformMisc::RequestExitWithStatus(1, 3)
[2023.04.06-14.25.52:308][ 0]LogCore: Engine exit requested (reason: Win RequestExit)
[2023.04.06-14.25.52:352][ 0]Log file closed, 04/06/23 09:25:52

I know this is a lot of text, but I really am at a loss. I really want to know if I should just fail my class and retake it again doing something that will actually work. Any help is immensely appreciated.

These are my questions and general lack of understandings:
Why did this happen in the first place? I want to know so that I avoid something like this ever happening again, and I’m kind of terrified of it happening again with this project. It’s really soiling my Unreal experience. I normally really enjoy debugging, but this pushed me too far.
Why would Unreal compile code that would break it this badly?
Why does Unreal still crash at the line where the first problem lines of code was if it has been rebuilt without those lines of code? Does Unreal somehow flag that in a way that the only way to clear the tag is to remove the script, rebuild, add the script, rebuild?

My best guess is that it is a problem with your enum and how you declared it. Enums in Unreal, like classes, structs, etc, are made in a specialized way so that they work with Unreal’s reflection system. Things that are not created with this in mind will be garbage collected at some point, which is why you are getting a null reference error in your stack trace.

FumbleCharacter.cpp:12 is the smoking gun that shows you the problem lies on line 12.

Here is an example of how to properly make an enum:

Thanks genuinely for the response! This is really good to know for enums, but I’m confused as line 12 in the .cpp file doesn’t refer to the enum at all. Normally, it has this line:

SuperJumpWidgetInstance = CreateWidget<UUserWidget> ( GetWorld ( ), SuperJumpWidget );

and then after removal that twelfth line becomes this:
charmovement = GetCharacterMovement ( );

In fact, that function doesn’t have the enum in it at all. I also don’t have to remove the enum definition from the .h file when re-importing in order for it to work. Is this way of declaring the enum really possible of breaking code completely unrelated to it?

Ok, well it was line 12 in the code you pasted.

Next time, it would be a better idea to post the full code or a zip file with the project instead of cutting out parts of the code that you think are irrelevant. Then we can get to the root cause instead of trying to fix what you think is wrong with it. This sounds like a classic case of the XY problem.

I examined your code in more depth and I see some other issues.

You are creating the widget in the constructor of the class. It should be created in BeginPlay. In fact, all of these three lines:

charmovement = GetCharacterMovement ( );
PickupRange = this->GetOwner ( )->FindComponentByClass<USphereComponent> ( );
charamesh = this->GetOwner( )->FindComponentByClass<USkeletalMeshComponent> ( );

should not be included in the constructor because the other components might not exist yet.

This is the main reason why your project crashed before you got the editor to open. It tried to access things in the constructor that didn’t exist when Unreal tried to create the CDOs.

Also, when creating the widget, you need to do a null check.

Instead of this:

SuperJumpWidgetInstance = CreateWidget<UUserWidget> ( GetWorld ( ), SuperJumpWidget );

Do this:

if (SuperJumpWidget )
{
   SuperJumpWidgetInstance = CreateWidget<UUserWidget> ( GetWorld ( ), SuperJumpWidget );
}

Why would Unreal compile code that would break it this badly?

Unreal didn’t compile the code. Your IDE did. You made valid C++ but you still don’t understand how Unreal’s system works. Unreal’s C++ is much different than standard C++.

You seemingly misunderstand Unreal’s use of constructors with Unreal types. The constructor is used to create the Class Default Object (CDO). When the engine initializes, a CDO is created for every uobject type defined. This is created via the constructor. Now, due to the timing of when CDOs are created, if something you put in your constructor crashes, it will crash before opening the editor.
Also if you instantiate a uobject at runtime (say, via CreateWidget), it is my understanding that the CDO is copied to create this new instance. So, let’s say CreateWidget in the constructor actually worked. You would have a widget instantiated in the CDO. Then you would spawn your player in the world, and it would copy the pointer to that CDO widget into your new player. That could be ok, but what happens if you make another player? They would both refer to the same widget on the cdo. That is not what you would want in general.
Further, CreateWidget in the constructor is crashing, likely because some underlying engine systems for instantiating widgets have not yet been instantiated themselves. Why it continues to crash after removing the code is likely some corrupted serialized data issue. Hard to say.

The solution is to use the BeginPlay to create your widgets, and anything else that is runtime specific to an individual player.