The game I work on spawn a lot of actors at runtime. As the amount of actors and memory usage started to get high (over 2GB), I coded a clean up function that discard useless actors.
Everything work, and this allow me to keep the memory usage at a fixed amount.
Except that the actors’ counter in the scene outliner reach a negative number.
No crash, no error in the output. Just this negative number.
I’m fairly new to Unreal, and I don’t know what cause the counter to behave like that. I don’t even know where the counter fetch the negative value.
If you look at how TotalActorCount is modified, you’ll see it is incremented or decremented each time an item is added or remove. When I destroy a bunch of actors at once, the counter get out of sync by trying to refresh and then remove item.
What I don’t understand is that ActorToTreeItemMap hold an accurate counter of actors in the TreeView. Replacing the value of TotalActorCount by ActorToTreeItemMap.Num() solve my problem. What is the purpose of TotalActorCount?
I manage to reproduce the issue with a few modification to the “Code First Person” template.
The spawning and cleanup :
//ATempActor is an AActor with a RootComponent
//Actors is std::vector<AActor*>
ATempActor* tempActor = World->SpawnActor<ATempActor>(ATempActor::StaticClass());
if (!Actors.empty())
{
tempActor->AttachRootComponentToActor(Actors.back());
}
Actors.push_back(tempActor);
if (Actors.size()>10)
{
// each time, Scene outliner lose 19 actors, instead of 10
for (auto * actor : Actors)
{
actor->Destroy();
}
Actors.clear();
}
I tried doing some testing on this, but was unable to see the actor counter display negative values. Could you please send the code you used in the first person template to reproduce this issue?
Generate a project named ActorCounter using the “Code First Person” template. Replace the content of the “Source” folder with the attached zip. source.zip
I don’t know if there is a better way to send you the code.
There is a new class ATempActor, and some changes inside AActorCounterCharacter::OnFire().
Start the game, you should have 41 actors. Click 11 times to run the OnFire() method. Counter will drop to 32. Continue until you reach negative.
Thank you for providing the information and code for this issue. I was able to reproduce it and have submitted a report about the issue to the development team for further investigation.
We have a report in place for this issue (TTP# 340804) and our development team is aware of it. Unfortunately I cannot make any estimates with regards to when this will be resolved.