Download

Crash when i add element to TMap<int32, int32> in USaveGame class

Hi, i have such problem:

I’ve created class UAHSaveGame : public USaveGame
with public variable TMap<int32, int32> Levels;

Creating mechanism is: UGameplayStatics::CreateSaveGameObject(UAHSaveGame::StaticClass());

When i run my game as Standalone game, and call Levels.Add(0,0); from Widget
game crashes

SEGV_NOOP at 0x0

FSetElementId TSet<TTuple<int, int>, TDefaultMapHashableKeyFuncs<int, int, false>, FDefaultSetAllocator>::Emplace<TPairInitializer<int&&, int&&> >(TPairInitializer<int&&, int&&>&&, bool*) Address = 0x16a59372e [/Users/Shared/EpicGames/UE_4.18/Engine/Source/Runtime/Core/Public/Containers/SparseArray.h, line 110] [in UE4Editor-MyProject-4716.dylib]

I doubt TMap is broken, instead it’s likely that your UAHSaveGame pointer is either invalid, or was cleaned up.

How are you holding on to that pointer?

You should have a UPROPERTY holding it somewhere, so it doesn’t get Garbage Collected.



UPROPERTY()
UAHSaveGame* MyCurrentSaveGame; // Because this pointer is a UPROPERTY, it's a "strong reference" and will prevent the object from getting Garbage Collected.


Hi, thanks for response.
Currently, I store UAHSaveGame in my GameInstance class, like
UAHGameInstance : public UPlatformGameInstance
with
private:
UAHSaveGame* GameSave;

Really, i have used it without UPROPERTY macro.

And it seems that macro fixed my bug.
Thanks, you are a lifesaver.

That’s the problem. Your GameSave will get garbage collected because the GC will say “Well, no one is holding on to this - so, nuke it”. You need to mark it as a UPROPERTY. If you don’t want the property to be saved out, just mark it as UPROPERTY(Transient).

Big thanks to you. You make my day!

Thank you. your my hero!!