Game crashes when saving

Every time I call SaveGame() game crashes. Any idea why?

MouseGameInstance.h

#pragma once

#include "CoreMinimal.h"
#include "Engine/GameInstance.h"
#include "MouseGameInstance.generated.h"


UCLASS()
class AMAZINGMOUSERUNNER_API UMouseGameInstance : public UGameInstance
{
	GENERATED_BODY()

public:	
	UMouseGameInstance();

	class UMouseSaveGame* MouseSaveGame;
	FString SaveSlot = "Save1";

	UFUNCTION(BlueprintCallable)
	void LoadGame();
	UFUNCTION(BlueprintCallable)
	void SaveGame();

	int Coins= 0;
	int Shields = 0;

	int FenceType = 0;
	bool FenceLockState[3] = {true, false, false};

	UFUNCTION(BlueprintPure)
	int GetCoins() { return Coins; }

	UFUNCTION(BlueprintPure)
	int GetFenceType() { return FenceType; }

	UFUNCTION(BlueprintCallable)
	void SetFenceType(int NewType) { FenceType = NewType; }

	void AddCoin() { Coins++; }
};

MouseGameInstance.cpp

#include "MouseGameInstance.h"
#include "MouseSaveGame.h"
#include "Kismet/GameplayStatics.h"


UMouseGameInstance::UMouseGameInstance()
{
    MouseSaveGame = Cast<UMouseSaveGame>(UGameplayStatics::LoadGameFromSlot(SaveSlot, 0));

    UE_LOG(LogTemp, Error, TEXT("Working..."));

    if(MouseSaveGame == nullptr)
    {
        MouseSaveGame = Cast<UMouseSaveGame>(UGameplayStatics::CreateSaveGameObject(UMouseSaveGame::StaticClass()));

        MouseSaveGame->Coins = 0;
        MouseSaveGame->FenceType = 0;

        UGameplayStatics::SaveGameToSlot(MouseSaveGame, SaveSlot, 0);

        
    }
    LoadGame();
}

void UMouseGameInstance::LoadGame()
{
    Coins = MouseSaveGame->Coins;
    FenceType = MouseSaveGame->FenceType;
}

void UMouseGameInstance::SaveGame()
{
    if(MouseSaveGame)
    {
        MouseSaveGame->Coins = Coins;
        MouseSaveGame->FenceType = FenceType;

        UGameplayStatics::SaveGameToSlot(MouseSaveGame, SaveSlot, 0);
    }
}

Crash log

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0xffffffffffffffff

UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_AmazingMouseRunner_0033!UMouseGameInstance::SaveGame() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseGameInstance.cpp:43]
UnrealEditor_AmazingMouseRunner_0033!AMouseController::Death() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseController.cpp:37]
UnrealEditor_AmazingMouseRunner_0033!AMouseCharacter::Death() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseCharacter.cpp:205]
UnrealEditor_AmazingMouseRunner_0033!ABasePlatform::DeathOverlap() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\BasePlatform.cpp:114]
UnrealEditor_AmazingMouseRunner_0033!ABasePlatform::execDeathOverlap() [D:\UE5\nauka\AmazingMouseRunner\Intermediate\Build\Win64\UnrealEditor\Inc\AmazingMouseRunner\UHT\BasePlatform.gen.cpp:37]
UnrealEditor_CoreUObject
UnrealEditor_CoreUObject
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_AmazingMouseRunner_0033!AMouseCharacter::Diving() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseCharacter.cpp:113]
UnrealEditor_AmazingMouseRunner_0033!AMouseCharacter::Tick() [D:\UE5\nauka\AmazingMouseRunner\Source\AmazingMouseRunner\MouseCharacter.cpp:49]
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_Engine
UnrealEditor_UnrealEd
UnrealEditor_UnrealEd
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
UnrealEditor
kernel32
ntdll

Pointers to UObjects need to have the UPROPERTY macro so that the reflection system & therefore the garbage collector can see them. WIthout that, the garbage collector doesn’t realize that the object is still in use, so it destroys the save object.

Thanks. It’s not crashing anymore, but there is another problem. When SaveGame() function is called MouseSaveGame is nullptr even though I create it in UMouseGameInstance(). Do you know why is this happening?

So I tried to LoadGameFromSlot() again in SaveGame() and it’s working but I’m still curious why wasn’t it enough to Load it in constructor.

There’s probably a timing thing because you shouldn’t be doing those thing in the constructor. You want to be doing all that save game stuff by overriding the Init function instead.

Thank you again

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.