Widget component widget not garbage collected

If I add a widget component and then create a widget and set the widget in the component, then the widget is not garbage collected.

Note: The bottom “add widget component” doesn’t have a widget class set.

Hello DekanTrue,

This is working as intended, if a widget is being referenced in a UObject it will remain in memory. I hope that this information helps.

Make it a great day

Hi Rudy,

Two things I don’t understand then:

  1. How is the bottom example storing a reference to the widget, and the top example is not?
  2. Upon destroying the actor, shouldn’t all references to the widget component disappear. The widget is still not garbage collected upon actor deconstruction. I have tried to remove widget from parent and remove the reference to the widget component with no result.

Neither widget will be removed from memory. One test you can run would be to use get all widgets of class and uncheck “Top Level Only”. This a quick way to see how many of a certain widget are still in memory.

I have made that test yes. That is how I noticed the issue.
Upon actor deconconstruction, if I force garbage collection, then the widget in the top example is cleaned. The bottom one is one.

A widget that is created using Create widget will look for the player, then the world, then the game instance. This means that unless all of them are removed (such as when the level is changed) the widget will continue to be referenced. As long as the widget is referenced, it will not be garbage collected.

Why would it be intended to work that way? It just uses memory.