x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

Prevent garbage collecting, how to RemoveFromRoot ?

Hello,

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.

 AClientCharacter::~AClientCharacter()
 {
     DifferentMix->RemoveFromRoot();
 }

 

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

Product Version: UE 4.13
Tags:
more ▼

asked Oct 12 '16 at 04:56 PM in C++ Programming

avatar image

Syntopia
222 22 27 45

avatar image Doug E ♦♦ STAFF Oct 12 '16 at 06:46 PM

Hey Syntopia-

Can you elaborate on what exactly you're trying to accomplish? If possible please provide further source context for the class that you're referring to in regard to how you're creating and using it. Without fully understanding what your goal is, the one suggestion I would make is to try using the class' BeginDestroy() function rather than a class destructor.

avatar image Syntopia Oct 12 '16 at 08:49 PM

I have UObject (DifferentMix) with "kludge" and different tools like ProgressBar widget. For this UObject I need already created world, I want use in it a timer, so I create an object in ACharacter inherit class (MyCharacter).

This MyCharacter is empty, but present in the world and seems to be destroyed in a minute GC (fires EndPlay). My Character has:

 static UDifferentMix * DifferentMix;

through this variable I get access to my tools from anywhere in the program, which may be required at an arbitrary time, for example, the ProgressBar widget.

 void AClientCharacter::BeginPlay()
 {
     Super::BeginPlay();
 
     DifferentMix = NewObject<UDifferentMix>(this, UDifferentMix::StaticClass());
     DifferentMix->AddToRoot(); // prevent garbage collection after some time unused
     DifferentMix->Init();
 }

The problem is that the GC removes DifferentMix and apparently MyCharacter through a minute idle. RemoveFromRoot() works, but only in EndPlay() as it is called after a minute, in the end it does not make sense. Can I prevent the removal of MyCharacter? call UObjectBaseUtility::AddToRoot()? How correctly do it?

Or prevent the removal DifferentMix after a minute? AddToRoot() works, but I do not know where to make RemoveFromRoot().

BeginDestroy() leads to crash engine on start with error:

 Fatal error: [File:D:\Build\++UE4+Release-4.13+Compile\Sync\Engine\Source\Runtime\CoreUObject\Private\UObject\Obj.cpp] [Line: 742] 
 REINST_ClientCharacter_BP_C_9 /Engine/Transient.TRASH_Default__ClientCharacter_BP_C_0 failed to route BeginDestroy

Thank you.

(comments are locked)
10|2000 characters needed characters left

2 answers: sort voted first

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.

more ▼

answered Mar 28 '18 at 11:08 AM

avatar image

LLILAK0BLOCK
79 2 6 8

avatar image Yaki Studios Apr 05 '19 at 11:26 AM

this is the correct answer. You set Rf_Standalone when creating the UObject and remove the flag when you want your UObject garbage collected. Easiest and cleanest way

(comments are locked)
10|2000 characters needed characters left

Hey Syntopia-

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.

Cheers

Doug Wilson

more ▼

answered Oct 13 '16 at 03:49 PM

avatar image LLILAK0BLOCK Mar 28 '18 at 10:46 AM

This is wrong. UObjects MarkIsPendlingKill() will fire before Characters EndPlay(), so the RootSetFlag wont be cleared, and the engine will crash.

(comments are locked)
10|2000 characters needed characters left
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question