Why am I getting a 'this' pointer invalid error?

Hey everyone. I’m trying to create a custom Game Instance class in C++, but I’ve run into a bit of a problem.

After compiling my class, my log outputs a copy of 4 “LogUObjectBase: Error: ‘this’ pointer is invalid.” errors.

I saw this post earlier, but it hasn’t seemed to help much. I’ve double checked my class and I seem to have all of my variables and methods properly macro-ed with UPROPERTY() and UFUNCTION(). ShadowRiver mentioned using the RF_RootSet flag to prevent a class from being garbage collected, but:
A) I can’t see to find this flag in VS for Unreal Version 4.18
and
B) I’m pretty sure a standard Game Instance class shouldn’t need this to avoid garbage collection ( though I could be wrong). Pasting my code below. Can someone explain what I’m doing wrong and why?

My Class:
h

#pragma once

#include "UMG.h"
#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "FastTT_GameInstance.generated.h"

/**
 * 
 */
UCLASS()
class GAMEOFCLONES_API UFastTT_GameInstance : public UGameInstance
{
	GENERATED_BODY()
public:
	DECLARE_EVENT(UFastTT_GameInstance, ShowLobbyGUI)
	UFastTT_GameInstance();

protected:

	/**
	* The UMG GUI widget to be displayed for the game's main menu
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widget Templates")
	TSubclassOf<UUserWidget> MainMenuGUIWidget;
	/**
	* Reference to instantiated Main Menu GUI Widget
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
	UUserWidget* MainMenuWidgetRef;


	/**
	* The UMG GUI widget displayed in the game's multiplayer lobbys
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widget Templates")
	TSubclassOf<UUserWidget> LobbyGUIWidget;
	/**
	* Reference to instantiated Lobby GUI Widget
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widgets")
	UUserWidget* LobbyWidgetRef;

	/**
	* The UMG GUI widget used to select settings for the game session to be 
	* created. Should have options such as game type, map, num players, etc.
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widget Templates")
	TSubclassOf<UUserWidget> HostSettingsGUIWidget;

	/**
	* The UMG GUI widget used to select a server (ie. game session) to join Should be displayed on a client trying to pick and join a game session
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Widget Templates")
	TSubclassOf<UUserWidget> ServerListGUIWidget;

	/**
	* The maximum number of players allowed to join the game.
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Settings")
	int32 MaxPlayers;

	/**
	* The name of the session to create
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Settings")
	FText ServerName;

	/**
	* Name of the UMap used as the game's lobby
	*/
	UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = "Game Settings")
	FName LobbyLevelName;



public:
#pragma region Accessors and Mutators
#pragma region GUI
	/**
	* Gets a pointer to the UMG widget containing the GUI that is
	* displayed in multiplayer session lobbies.
	* @see UWidget* UFastTT_GameInstance::LobbyGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> GetLobbyGUIWidget();

	/**
	* Set a pointer to the UMG widget containing the GUI that is
	* displayed in multiplayer session lobbies.
	* @see UWidget* UFastTT_GameInstance::LobbyGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> SetLobbyGUIWidget(TSubclassOf<UUserWidget> NewWidget);

	/**
	* Sets a pointer to the UMG widget containing the GUI that is
	* displayed as the game's main menu.
	* @see UWidget* UFastTT_GameInstance::MainMenuGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> GetMainMenuGUIWidget();

	/**
	* Sets a pointer to the UMG widget containing the Host Settings GUI that is
	* used to set session settings such as as game type, map, num players, etc.
	* @see UWidget* UFastTT_GameInstance::HostSettingsGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> SetMainMenuGUIWidget(TSubclassOf<UUserWidget> NewWidget);

	/**
	* Gets a pointer to the UMG widget containing the Host Settings GUI that is
	* used to set session settings such as as game type, map, num players, etc.
	* @see UWidget* UFastTT_GameInstance::HostSettingsGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> GetHostSettingsGUIWidget();

	/**
	* Sets a pointer to the UMG widget containing the Host Settings GUI that is
	* used to set session settings such as as game type, map, num players, etc.
	* @see UWidget* UFastTT_GameInstance::HostSettingsGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> SetHostSettingsGUIWidget(TSubclassOf<UUserWidget> NewWidget);

	/**
	* Gets a pointer to the UMG widget containing the Host Settings GUI that is
	* used to set session settings such as as game type, map, num players, etc.
	* @see UWidget* UFastTT_GameInstance::HostSettingsGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> GetServerListGUIWidget();

	/**
	* Sets a pointer to the UMG widget containing the Host Settings GUI that is
	* used to set session settings such as as game type, map, num players, etc.
	* @see UWidget* UFastTT_GameInstance::HostSettingsGUIWidget
	*/
	UFUNCTION(BlueprintCallable)
	TSubclassOf<UUserWidget> SetServerListGUIWidget(TSubclassOf<UUserWidget> NewWidget);

#pragma endregion


#pragma endregion

	/**
	* Shows or hides the MainMenuGUIWidget by attaching the LobbyGUIWidget to the parent view.
	*/
	UFUNCTION(BlueprintCallable)
	void ShowMainMenuGUI(bool show);

	/**
	* Shows or hides the LobbyGUIWidget by attaching the LobbyGUIWidget to the parent view.
	*/
	UFUNCTION(BlueprintCallable)
	void ShowLobbyGUI(bool show);

	/**
	* Fills GUI widget variables with instances of the widget classes specified.
	*/
	UFUNCTION(BlueprintCallable)
	void PopulateWidgetInstances();

	/**
	* Checks if Ref is already filled and valid, and if it isn't creates a new
	* Widget of the template classand returns a UUserWidget pointer to it.
	*/
	UFUNCTION()
	void CreateWidgetFromTemplate(TSubclassOf<UUserWidget> templateWidget, UUserWidget* ref);

	/**
	*
	*/
	UFUNCTION()
	void ToggleGUIWidget(bool show, UUserWidget* WidgetToToggle);


private:

	
	
};

c++

#include "FastTT_GameInstance.h"

UFastTT_GameInstance::UFastTT_GameInstance()
{
	this->SetFlags(RF_);
	PopulateWidgetInstances();
}


#pragma region Accessors and Mutators

#pragma region GUI

TSubclassOf<UUserWidget> UFastTT_GameInstance::GetLobbyGUIWidget()
{
	return LobbyGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::SetLobbyGUIWidget(TSubclassOf<UUserWidget> NewWidget)
{
	LobbyGUIWidget = NewWidget;
	return LobbyGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::GetMainMenuGUIWidget()
{
	return MainMenuGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::SetMainMenuGUIWidget(TSubclassOf<UUserWidget> NewWidget)
{
	MainMenuGUIWidget = NewWidget;
	return LobbyGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::GetHostSettingsGUIWidget()
{
	return HostSettingsGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::SetHostSettingsGUIWidget(TSubclassOf<UUserWidget> NewWidget)
{
	HostSettingsGUIWidget = NewWidget;
	return HostSettingsGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::GetServerListGUIWidget()
{
	return ServerListGUIWidget;
}

TSubclassOf<UUserWidget> UFastTT_GameInstance::SetServerListGUIWidget(TSubclassOf<UUserWidget> NewWidget)
{
	ServerListGUIWidget = NewWidget;
	return ServerListGUIWidget;
}


#pragma endregion


#pragma endregion

void UFastTT_GameInstance::ShowMainMenuGUI(bool show)
{
	ToggleGUIWidget(show, MainMenuWidgetRef);
}

void UFastTT_GameInstance::ShowLobbyGUI(bool show) 
{
	UE_LOG(LogTemp, Warning, TEXT("ShowLobbyGUI"));
	ToggleGUIWidget(show, LobbyWidgetRef);
}

void UFastTT_GameInstance::ToggleGUIWidget(bool show, UUserWidget* WidgetToToggle)
{
	if (!WidgetToToggle->IsValidLowLevel())
	{
		PopulateWidgetInstances();
		if (!WidgetToToggle->IsValidLowLevel())
		{
			UE_LOG(LogTemp, Error, TEXT("ToggleGUIWidget was called with an invalid WidgetToToggle"));
			return;
		}
	}

	if (show == false)
	{
		WidgetToToggle->RemoveFromViewport();
	}
	else
	{
		WidgetToToggle->AddToViewport();
	}
}

void UFastTT_GameInstance::PopulateWidgetInstances()
{
	CreateWidgetFromTemplate(MainMenuGUIWidget, MainMenuWidgetRef);
	CreateWidgetFromTemplate(LobbyGUIWidget, LobbyWidgetRef);
}


void UFastTT_GameInstance::CreateWidgetFromTemplate(TSubclassOf<UUserWidget> templateWidget, UUserWidget* ref)
{
	//Check if widget template has a valid value
	if (templateWidget->IsValidLowLevelFast())
	{
		if (ref->IsValidLowLevel())
		{
			//Do Nothing, Reference to an existing widget already exists
		}
		else
		{
			ref = CreateWidget<UUserWidget>(this, templateWidget);
		}

	}
	else if (!LobbyGUIWidget->IsValidLowLevelFast())
	{
		
	}
	else
	{

	}
}

I remember seeing this problem but I can’t remember what I did. But to clarify: you are seeing this during compile, correct? The post you linked seems to happen at runtime.

I’m guessing runtime. I think it’s happening because of how the object is being used. The problem isn’t in the code for the class. ‘this’ should be valid for sure. So please show us the object is being created and used.

Could you share the full output log ?

you have any lines number ?

it’s a compilation error, not a runtime error right ?

by commenting the methods / properties, you suceed to build ?
if yes, un comment part by part to isolate the faulty part :slight_smile:

The error appears to be occuring during hot reload. I have no problem compiling my project, but during hot reload I get the issues.
VS Output: 1>------ Build started: Project: UE4, Configuration: BuiltWithUnrealBuildTool Wi - Pastebin.com
Unreal Output Log: Unreal Output Log - Pastebin.com
(apologies for the delay, fighting fires).

When a project or game is loaded it definitely hits the constructor more than once. I assume that’s to create the default object and such things to set things up. But it’s still weird that ‘this’ is invalid. Maybe that’s actually because it’s just the default object.

But there’s nothing in the error that says precisely where it’s happening so I suppose it might be happening in the widget. It could have something to do with the fact that there’s no HUD at that point so widget creation might fail. Either way, it’s probably not a good idea to run that code from the constructor.

First, I’d try creating my widget stuff in BeginPlay() or somewhere else where you can be sure the game and HUD system are running. And I’d pull out that call to SetFlags().

  1. Does the same thing happen if you close the editor, compile, and reopen?
  2. Does it happen when you run the game in the editor?

It happens during Hot-Reload, immediately following the “LogHotReload: Starting Hot-Reload from IDE” line.

It happens at the start of PIE, immediately after loading the level, but then never recurs.

It also is still happening after closing and reopening the project in both UE4 and VS

*It Happens = I see four copies of the “LogUObjectBase: Error: ‘this’ pointer is invalid.” error.
This doesn’t appear to be crashing my build or anything, but I’m trying to keep my project as error free as possible, so I’d like to find the source and figure out a resolution

I’ll try shuffling the code around a bit more per your suggestions.

I think it just clicked for me and I know what’s happening. The default object is being created before the full system is up, so while ‘this’ is a valid pointer, it isn’t a valid UE4 object. Moving the widget creation to BeginPlay() should solve the issue.

Unfortunately, the thought just occurred to me that I can’t move the code to BeginPlay()… This code is inside of a GameInstance class, which doesn’t derive from AActor.
I moved the InstantiationCode (ie. my call to “PopulateWidgetInstances”) from the constructor to UFastTT_GameInstance::OnStart() and it fixed the errors that were occurring during Hot Reload. The other errors are still present. Edit: On further review of my code I have found that PopulateWidgetInstances doesn’t appear to be related to the other pointer errors, as commenting it out entirely still produces the errors whenever I play-in-editor.

Good point. GameInstance isn’t quite where I would think to put a widget. Is there a reason why you need it there?

I’d either put the HUD stuff in the player actor or call the game instance setup function from BeginPlay in the player actor. But I just got an email and it seems you got it working so if you prefer to keep it where it is that’s cool.

Gameinstance have an init method, you can hook it there: ( don’t forget the Super in implementation )
virtual void Init() override;

Log doesn’t indicate anything about game instance, if you simply remove your implementation of game instance, do the bug/log disapear ? ( maybe you changed something else completely unrelated

Also, fully remove you constructor override or use the UE one it handle a part of hot reload