x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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
     {
 
     }
 }

Product Version: UE 4.18
Tags:
more ▼

asked Jan 31 '18 at 04:09 PM in C++ Programming

avatar image

The7thCrest
120 13 19 22

avatar image Firefly74 Jan 31 '18 at 04:28 PM

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 :)

avatar image Jin_VE Jan 31 '18 at 04:31 PM

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.

avatar image Jin_VE Jan 31 '18 at 04:41 PM

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.

avatar image The7thCrest Jan 31 '18 at 05:59 PM

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: https://pastebin.com/vCJu7ZXm Unreal Output Log: https://pastebin.com/Br1phACv (apologies for the delay, fighting fires).

(comments are locked)
10|2000 characters needed characters left

1 answer: sort voted first

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?

more ▼

answered Jan 31 '18 at 06:22 PM

avatar image

Jin_VE
4.3k 31 75 40

avatar image The7thCrest Jan 31 '18 at 07:12 PM

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.

avatar image Jin_VE Jan 31 '18 at 07:22 PM

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.

avatar image The7thCrest Jan 31 '18 at 07:41 PM

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.

avatar image Jin_VE Jan 31 '18 at 08:06 PM

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.

avatar image Firefly74 Jan 31 '18 at 09:34 PM

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

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question