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"

Office Holiday

Epic Games' offices will be on holiday from June 22nd to July 7th. During this period support will be limited. Our offices will reopen on Monday, July 8th. 

Garbage collection with UObjects

I have a UObject that I'm using as a "Damage Component".

 UCLASS(Blueprintable, BlueprintType)
 class MOS_API UMOSDamageComponent : public UObject

The damage components are instantiated and sent to our pawns to register in a list:

The pawn updates the damage components on tick and manages when they are deleted... or they're supposed to.

 void AMOSCharacter::Tick( float DeltaTime )
 {
     Super::Tick( DeltaTime );
 
     bool ItemsNeedingUnregister = false;
     
     for (int i = 0; i < RegisteredDamageComponents.Num(); i++)
     {
         //Check if this object isn't marked for deletion
         if (RegisteredDamageComponents[i]->IsValidLowLevel())
         {
             if (RegisteredDamageComponents[i]->bNeedsUnRegister)
             {
                 GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,
                     FString::Printf(TEXT("Component marked for un register")));
                 RegisteredDamageComponents[i]->OnUnRegister();
                 ItemsNeedingUnregister = true;
             }
             else
             {
                 if (RegisteredDamageComponents[i]->bNeedsTick)
                 {
                     RegisteredDamageComponents[i]->DamageComponentTick(DeltaTime);
                 }
             }
         }
     }
 
     //Attempt 1
     //if any components need unregistering, copy over to a new list (avoids crash)
     //https://answers.unrealengine.com/questions/272651/tarray-remove-function-call-cause-editor-crash.html
     if (ItemsNeedingUnregister)
     {
         TArray<UMOSDamageComponent*> NewComponentList;
 
         for (int i = 0; i < RegisteredDamageComponents.Num(); i++)
         {
             if (RegisteredDamageComponents[i]->IsValidLowLevel())
             {
                 if (!RegisteredDamageComponents[i]->bNeedsUnRegister)
                 {
                     NewComponentList.Add(RegisteredDamageComponents[i]);
                 }
                 else
                 {
                     RegisteredDamageComponents[i]->MarkPendingKill();
                     RegisteredDamageComponents[i] = nullptr;
                 }
             }
         }
 
         RegisteredDamageComponents = NewComponentList;
         GEngine->AddOnScreenDebugMessage(-1, 5.f, FColor::Red,
             FString::Printf(TEXT("Unregistering components. Component size is now %d"), RegisteredDamageComponents.Num()));
 
         //GetWorld()->ForceGarbageCollection(true);
     }
 }

The problem is, when I'm stress testing this I see my memory usage slowly climb over time. Am I doing something wrong in marking these for garbage collection? Even when I force garbage collection, my memory never stabilizes. Nor do I get a performance hit for forcing garbage collection every frame.

Product Version: UE 4.10
Tags:
more ▼

asked Apr 24 '16 at 09:27 PM in C++ Programming

avatar image

Gorlock
21 1 7 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

Sooo I updated to 4.11 and the problem seems to have gone away.

I would still like feedback on if I'm doing this correctly, but perhaps there was a bug fixed in 4.11?

more ▼

answered Apr 24 '16 at 11:06 PM

avatar image

Gorlock
21 1 7 8

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
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