Why does accessing Game Instance = crash

Greetings!

I made a new game instance object and configured the Unreal project settings to use it by default. Everything compiles fine.

In the game mode, I added the following:

UBlahGameInstance* BGI = Cast<UBlahGameInstance>(GetGameInstance());
UBlahGameInstance* BGI = Cast<UBlahGameInstance>(GetWorld()->GetGameInstance());

In both cases, the code compiles fine but when I run the Unreal editor, it always crashes with the following error:

Access violation - code c0000005 (first/second chance not available)

What is going on here … ?

Thank you.

Are you checking for null? It’s possible that the returned pointer is null

Thank you for the response.

From my understanding, I only need to check for null when referencing the returned pointer not when declaring it (is that even possible…?).

I can see a memory violation if I tried to access the pointer and there was nothing there. Here, the declaration is crashing Unreal 100% of the time.

GetWorld() can return null

Alright, this helped point me in the right direction. Basically, the issue was that I was calling the function in the game mode constructor for testing purposes. Long story short, GetInstance() didn’t exist, so the engine was crashing.

I created a test actor and was able to access the game instance.

Thanks!

Hi,

I’m using 4.8.3 and in my case I added the “GetGameInstance()” into a class constructor (dumb move), the code compiled fine and the editor stopped running and when it crashed it said nothing about the error not even the .log files had anything of use there.

I had to remove all my latest code and gradually adding it back in (a day of work in that).

Just wanted to add this to the thread as the behaviour of not showing any error nor logging the null pointer exception? or something like it shouldn’t be considered a bug?
Can anyone else validate that the crash dump doesn’t indeed say anything?

To reproduce just:
Add an AActor subclass;
In the constructor just add the GetGameInstance() into a local or class variable (it does the same).
Compile and launch the editor.

Thank you for the time and hopefully I’ve helped ironing a tiny bit of the engine! Or just be clueless at what I’m doing :stuck_out_tongue:

I just ran into the same issue as Farious. Crashes the editor on startup. No real help in the log file, but could see the GetGameInstance() was the cause of the crash.
Was also during it during the actor initialize.

.h
UPROPERTY(EditAnywhere, BlueprintReadWrite, Category = “Structure”)
UPOTLGameInstance* GameInstance;

.cpp
GameInstance = Cast(GetGameInstance()); <= Crash