UMG - Remove From Parent - still valid

Hi folks,

I’ve been trying to figure out if this is new to 4.20.3 but when I Remove From Parent on a widget, or Remove All Widgets, they seem to be removed from the viewport, but remain in memory and also the variable is still valid.
If i ignore it’s validity and Create Widget again I will have multiple versions of the same widget in memory - you can see this by dropping down the Debug Filter and seeing how many duplicates. This does not seem right at all. Imagine pausing the menu several times you would be using unnecessary memory storing duplicates. Is this a known issue to Epic? Anyone else having the same problems?
I don’t remember having this problem in previous versions of ue4.

Thanks,
Ming

1 Like

Hi Oolahk et al,

yes, a similar thing is happening to me right now.
But firstly I do suggest that every time you create a new Widget you check if it exists already with the “Is Valid” check and then just use that one rather than create a new one. This at least stops the duplication.

My situation is similar, but is creating bugs: When aborting the game level, I am removing a HUD blueprint (“Remove from Parent”), and loading the menu map; but the code within the HUD is still firing (I have some HUD animations and so on that use delays, e.g. a countdown). So if the player quickly re-enters the game level again things occur out of order.

I can see a quick hacky way to fix this but how is it supposed to work?

I believe this is a related bug that was submitted a few years ago.
https://issues.unrealengine.com/issue/UE-22560

It would be really good to hear what the intended procedure for this is supposed to be. I had assumed that Remove From Parent would stop all code running in that Widget. If not, what’s the best procedure for doing so? Does everyone Destroy widgets after a “Remove From Parent”? Is there a node that I’ve just not noticed for sorting this out?

Thanks,
Sabre.

After more intensive Googling, I’ve found the info that’s needed. Here:

link text

So “Remove from Parent” and then “Collect Garbage” should fix both of our issues. (Providing you move your “Create Widget” into a function).

Testing now… hmm, my first attempt doesn’t work. Let me know if you have more luck!

Thanks for your reply Sabre.

I checked out the post and found out that using “Collect Garbage” only works if you did not create a variable of the widget you add to the viewport. Upon further investigation, I figured out that my mentality is what’s incorrect. Instead of trying to remove it entirely, I just need to call it back later by adding to viewport again, vs trying to create it again. So for certain widgets that require variables, I check the validity and if it hasn’t already by created I create the widget, and if it is valid, then I would just add to viewport.
This will not cause duplicates either. I will now have to go through and check to make sure certain widgets like hit points are properly removed from memory by not storing them as variables. Thanks!

Thanks for your reply Sabre.

I checked out the post and found out that using “Collect Garbage” only works if you did not create a variable of the widget you add to the viewport. Upon further investigation, I figured out that my mentality is what’s incorrect. Instead of trying to remove it entirely, I just need to call it back later by adding to viewport again, vs trying to create it again. So for certain widgets that require variables, I check the validity and if it hasn’t already by created I create the widget, and if it is valid, then I would just add to viewport.
This will not cause duplicates either. I will now have to go through and check to make sure certain widgets like hit points are properly removed from memory by not storing them as variables. Thanks!

I got around this by set is enabled to false before remove from parent.
And then checking whether the widget is enabled if it passes the isvalid check.

1 Like

And what to do, if you have one variable of parent class and you want to reference with it multiple different widgets of child class? They are just lost in memory and never deleted.
Why is it intended to work that way? To waste memory?

It doesn’t work in any case.

This works, thank you!