Deleting Widgets

How do you properly delete widgets. Removing them from viewport or removing them from parent seems to still keep them in memory. For example I add widgets to a context menu. Close the context menu (and remove from parent/viewport) then I GetAllWidgetsOfClass that are not top level only and it will return the context menu widget and all it’s children still. They also don’t seem to be garbage collected even though they are UObjects.

If I manually call delete on them, then I can get rid of them but the engine will crash when it tries to remove them itself. I assume that this is because the child widgets are not destroyed and their outer is now invalid. Before I go and write a recursive function to destroy all the widgets manually, I figured I’d ask if this is already done somewhere. It seems like a bit of an oversight if every widget ever created is stuck in memory.

Deleting widgets isn’t in the engine? Really?

According to this post, you aren’t meant to delete widgets manually. If they have no references then they’ll destroy themselves through the GC system.

You could write some code that calls “ClearChildren” on the Widget if it’s a container, and tells its parent to remove the child itself (“RemoveChild / RemoveChildAt”), so the ref count reaches 0 and then GC will handle things.

1 Like

Yeah i was afraid of that. thank you for the link to the post. I’ll go ahead and create that recursive function. I’m surprised one does not exist in the engine.