Using an Array & UMG, am I making a memory leak?

I am using 4.10.1. I have a UMG widget attached to a controller’s viewport. I made a basic chat box, and it does work but for some reason after I have entered 10 things into the array (the size of elements is set to 10) there becomes a delay after I start the process of updating the array. This delay becomes bigger and bigger, going from barely noticeable at 10 entries to 10+ second freeze after 14 entries. The delay becoming so big so fast is probably due to my computer, but it is still building up which means there is something wrong with my blueprint but I cannot figure it out. This also happens if the game is packaged.

It is set up so that if you press enter inside the textbox it takes the input string, moves all of the strings in an array up 1 (removing the 0 index one), adds the new string, sets the textbox to be empty and each array value is displayed by one of the 10 boxes. There are two arrays, the one I care about and an empty one because if I try to edit the original while using the ForEachLoop it gives me an infinite loop error.

Why is this build up delay happening and getting worse when I update & move around these two arrays? It is only 10 elements. Again, this DOES work but after 10+ entries the delay becomes noticeable and seems to get almost twice as bad each time until it almost 100% freezes.

The UMG layout.

UMG event graph

Playercontroller, this is where the functions are and they are just called by a custom event.

String array updating component, probably the problem.

Adds new string to string array

Hmm, not quite sure why this is happening. I don’t see any code there that removes the old items/messages from the array. Run a print string of the length of the array to ensure you are only keeping 10. Also, don’t run the pin from the “Set Temp Array” into the “Insert” inside the loop. This could be the problem because every time the loop runs an iteration, it draws from that “Set” in order to insert the new string. What that means is every time it loops, that “Set” will need to reset itself from what supplies it. Just run a regular “Get Temp Array” into the insert node.

you are replicating an array of strings, which can be extremely expensive. and you are using array insert inside a loop, so every pass requires every element to be copied and moved, possibly creating and destroying its own temp variables in the process under the hood for every element, for every step of the loop.

why not just call a multicast function that adds the newest string to all clients, and let everyone keep track of their own, non-replicated, array of strings?:

client: onTextCommited, call a reliable function on the server that sends the new string.

server: when you receive a new string, call a reliable multicast function that sends the string to all clients.

client: if TextStrings.Length > 10, RemoveIndex 0. either way, Add NewString to TextStrings.

that way, you don’t need to replicate any variables, especially not arrays of strings, which are expensive.

Turns out the insert array node is the cause of the problems. Set array element node works without any issue. I will give your ideas a shot later on though. Thanks for the reply!