How to destroy UMG widget?

Hi, How can I destroy UMG widget? If I call “Remove from Parent” it stil exists in memory and I see in the “Debug Filter” in Blueprint…

Well, my first question would be why you would want to “destroy” the widget? Why not just hide and un-enable it?

I have not found the way to destroy/remove particular widget directly, instead I use “Clear children” on parent widget.
However, absence of destroy widget is really weird, considering “Remove from parent” keep object in game.

O_o Really? “Why destroy objects if you can hide them”? :smiley:

Use node remove from viewport

I not saying it shouldn’t be possible but if you’re not aiming at improving core performance it shouldn’t be necessary?

I myself do remove some widgets when they are not needed but however you get your preferred result it should be done in a way where you know what is happening and what you can/cannot do after said action.

I would think the Garbage Collection system would handle it for you?

There’s no explicit destruction call in UMG. If the widgets are either referenced by the ref counted slate widgets, or by the GC’ed UObject system it will remain in memory.

When building a radar with blueprints, it should would be nice to both create and destroy widgets during run time.

Memory leaks are much more likely without garbage collection, as you have to manually destroy everything.

Hopefully this will shed some light on the memory management in ue4.

It is possible to manually initiate garbage collection for a specific uobject if that’s what you would like. (This requires C++ however I believe)

if you still dont know i could show you

Video unavailable.

Found this thread while trying to solve a UMG problem. I hate to drag it up but directly destroying UMG elements should be a thing.

I currently have a widget that needs to remove itself and its parent from the viewport. There is no way to do this. Parent -> Remove From Parent doesn’t work. There’s no such thing as “remove from viewport” in the engine source. This is an unhandled use case.

Antidamage +1

Created a little test scenario using the floating combat text tutorial. Spawned 10,000 widgets across the screen from a keypress event, which creates the widget, adds to viewport and does nothing else. Widget on construct event plays its fade anim (deleted the tick event) and binds to animation finished event that calls Remove from Parent. On 1st keypress spawn of 10,000 widgets FPS drops from 55 to 2. After anims finish FPS recovers to 49. On 2nd keypress FPS recovers to 27. On 3rd keypress FPS recovers to 18. This is a strange but repeatable sequence. Without Remove from Parent FPS recovers to 13, then 7, then 4. Remove from Parent performs some kind of cleanup but still leaves the game with degraded performance.

I have this problem with my inventory. Hovering over an object spawns a tooltip, that displays the item and the one equipped. However, UMG is so “intelligent”, that when I remove the equipped item, and hover back over the inventory item, it still displays the equipped item. If I wait for a while, it fixes itself (or if I have a multi-slot item and a slot I didn’t hover over previously, it works perfectly). I would really like to forcibly destroy the former tooltip to get rid of these ghosts widgets.

I have to have all my widgets on a persistant level and was wondering if this code in 4.23 would free up memory

or will it still be there taking up memory?

I know this is kind of old, but it doesn’t seem to offer a solution and I was having a similar problem, so here is my solution:

Within the UMG event graph, you can add a node called ‘Remove All Widgets’. It looks like it is private, so you will need to create a function or event (I made an event called ‘SelfDestruct’). This will release all references which in turn will cause the UMG to call ‘Destruct’ (I put a print statement in my Event Destruct to be notified when it happens). This should fix the memory and FPS issues.

Thought I’d add to this as well, for those who may not want to remove all widgets at once. You can just remove your widget from the viewport, set the reference to NULL and then call “Collect Garbage” and it will be destroyed.

Depending on when you call “Collect Garbage”, you might end up hitching the game, though. So unless you’re doing it on a loading screen or something, it would really be better to just hide/unhide widgets as needed and then call “Remove All Widgets” if necessary when you no longer need any of them.

1 Like

I want to try this but how do you set a variable to NULL with Blueprints?

PS. Who came up with the idea to have an “add to viewport” function but no corresponding “remove from viewport” function? :stuck_out_tongue:

I don’t use Blueprints for much, but just using the “Set” node for your object and not setting it to anything should do the trick.

And technically “RemoveFromParent” is “RemoveFromViewport” (the latter exists in C++, but it’s just a deprecated version of the former). It works fine provided that you aren’t calling “CreateWidget” over and over again. The main issue this thread seems to be pointing to is that there might be use cases where you really want the widget destroyed and cleared out of memory for performance purposes. Apart from that, I think the current system works fine provided things are set up in a way that works.