I am working on a mod for Conan Exiles which adds HUD navigation markers in certain situations. For example, when the player dismounts, it will add a marker which points to the location where the player dismounted the horse.
This means, in this situation, I am adding a widget to a canvas dynamically by calling “Create Widget” & “Add Child To Canvas”.
When the player mounts the horse again, I must remove the marker, of course, since it’s no longer valid. I do this by using the “Remove From Parent” function.
Now while this works, it turns out that the removed widget still remains in memory. I noticed this since in order to remove the widget, I am using the function “Get All Widgets Of Class” which also finds the non-visible, already removed ones. Googling for explicit widget destruction seems to unveal that it is not possible to explicitly destroy a widget, and that its not 100% clear if the garbage collector gets rid of it at some point.
I tried to call “Remove From Parent” & directly after “Collect Garbage”. I can see that the garbage collection is actually triggered, because in game I will get a small stutter, but the widget is not destroyed. It will still be found, and after mounting & dismounting the horse several times, I can easily get to the point where about 20 unused (and invisible) widgets are still being iterated during the removal of the (single) currently active marker, which must be removed from parent.
So this leaves me with mixed feelings; 1) there seems to be no way to iterate children of a canvas, so I can’t just skip the already removed widgets. Instead I must always iterate all widgets, 2) I am not sure if garbage collection actually gets rid of the widgets at some point, so after a longer gaming session players might end up with dozens of unsed and invisible widgets, which just does not feel right.
Can I do anything about this, or would this just be the way to go in UE4, and I can’t change it? Will it lead to memory issues actually, or will small widgets (32x32 PNG image) not occupy so much memory so it can be neglected?