Download

Integer of UClass/UObject changes randomly overtime.

Hello guys,
I’ve stumbled upon an issue where I can’t seem to figure out what exactly the issue causes. Let me try to explain it with a simple example :

I have a GameMode class, the GameMode will only be calling “GetAmountOfCars()” to fill in the widget.
In the game, we also have a CarManager UClass, deriving from UObject, having just a counter and a getter for the counter. We instantiate it with the BeginPlay function like, UCarManager* CarManager = NewObject<UCarManager>(); When we start the game, everything goes well, it has the value 0. At a random time, this value transforms into a value like -572662307. I have not set the counter anywhere, the getter is only called on the widget and on a few calculations. Am I doing something wrong here?

Thanks in advance!

Sounds like the object is being garbage collected. How are you holding on to the CarManager? Unless it’s assigned to a UPROPERTY field, it will be marked for garbage collection as no one is “owning” that reference.



// h
UPROPERTY()
UCarManager* MyCarManager; // Since this field is marked as a UPROPERTY, the GC system will see it and avoid cleaning up your pointer.

// cpp
MyCarManager = NewObject<UCarManager>();