Prevent garbage collecting, how to RemoveFromRoot ?
I have a UObject class with different tools, access to it is through the static variable (DifferentMix), I can not use UPROPERTY().
So I use DifferentMix->AddToRoot(), it works, but how to remove properly? If I do not do it, when I stop simulation the engine crash.
I'm trying to delete it in the destructor, but the engine does not start then, I initialize variable in BeginPlay(), i need already world exist.
And how to use UObjectBaseUtility::AddToRoot / UObjectBaseUtility::RemoveFromRoot ? From this question: https://answers.unrealengine.com/questions/493997/best-way-to-avoid-uobjects-garbage-collection.html
asked Oct 12 '16 at 04:56 PM in C++ Programming
Do not ever use AddToRoot() or SetFlags(RF_MarkAsRootSet) if you do not have a custom cleanup function that will remove this flag BEFORE exiting game (before MarkIsPendlingKill() by engine loop). Passing an UWorld pointer as Outer in NewObject() and a flag RF_Standalone - is enough to keep your UObject alive during gameplay and auto destruction on game end.
Calling the RemoveFromRoot() function for you UObject should be possible from your character's EndPlay() function. This will make the call as your character is being removed from the level and should prevent the crash you're seeing.
answered Oct 13 '16 at 03:49 PM
Follow this question
Once you sign in you will be able to subscribe for any updates here