Removing Widgets from parent doesn't clear from memory

Hello!

I’m trying to understand how to clear a widget from memory.

It’s my understanding you can’t really destroy them but once removed from the parent then the trash collector will get rid of it.

I have two circumstances i’ve been testing with

  1. I make a button that creates a widget of a class that is just a child class of user widget. the widget will self remove from parent on a button press. I can click the button to create a bunch and then hit the button to remove from parent on all of them. All the widget objects removed from the viewport in but looking at the debug drop down, it always keeps the most recently created object even after manually calling the trash collector. If i then create more and then close them, it will still always keep the most recently created one.

  2. I push a button the calls a function and passes a reference widget class. The reference widget class is a child class of an another widget class. In the function i create the object and add it to the view port and adjust the size and position. i then have a different button which gets all widgets of the parent class, and with a for loop removes them all from parent. so i can create a bunch and then with one button remove them all from parent. they do disappear from the viewport but i can still seem them in the debug drop down. Running the trash collector does nothing.

I don’t really understand either behavior. i have two different circumstances, neither acting how i expect. I obviously am missing some fundamental knowledge.

I found some old threads about this being a bug but it seems like thats resolved.

I could work around it but since I think the root of this is a case of me not understanding references well enough, i’m hoping to get some advise before moving on.

Remove from parent - removes a widget visually from another widget, but the widget object itself remains in memory.
If you store hard references to created widgets somewhere, this will not allow the garbage collector to clear the memory, even if you forcefully call Collect Garbage.
In addition, clearing the memory may occur with a delay even if it was forced.

If you create and delete widgets very often, perhaps you should just hide them and reuse them?