AddToRoot() Not working

I load a class using

auto Class = StaticLoadClass(UObject::StaticClass(), ANY_PACKAGE, "blueprint class reference");
Class->AddToRoot();
Class->SetFlags(RF_Standalone);
s_ClassesArrray.Add(Class);

The class is returned and is valid. However, when i try to access that array at a later time (about 10 seconds later) Class is nullptr. I can’t for the life of me figure out how to prevent garbage collection. This is being done inside a blueprint library and i’ve also tried setting the Outer as the APlayerController And UWorld. I’ve wasted 2 days on this. Thanks

It’s a blueprint function library and therefore has only static members. The ‘s_ClassesArray’ is a static member and therefor cannot be marked as UPROPERTY(). However, calling AddToRoot() should prevent garbage collection regardless, but it doesn’t.

I’m not exactly sure what are you doing here but I assume it’s a global variable that goes null? Is it a UPROPERTY()? GC will collect Unreal’s types if it’s not there.
At least it’s what did it for me when I had a similar problem.

Ok, solved my issue. I really feel stupid for spending a couple days on this one. I had a typo when trying to access the s_ClassesArray. I MEANT to be doing this

auto Class = s_CardClasses[INT64_MAX % GetNumCardClasses()];

But what I actually wrote was this

 auto Class = s_CardClasses[INT64_MAX & GetNumCardClasses()];

and seeing that i only have 1 element in the array, it obviously tried to access garbage data. So now everthing works as it should and AddToRoot() Does indeed work