Question on deleting or removing a control

AstroBrad

New member
I am working a on a script that creates a set of controls (e.g. edit, label, combobox, etc) based on input from a file. I want to be able to remove or delete these controls based input from a different file as they are no longer needed. I can not find anyway to remove controls that are no longer needed in my dialog. I know I can hide or make them invisible but they are still in memory. I think removing them from a sizer will still have them in memory. Any ideas on how to delete/remove controls from memory? This seems like a common operation in most GUI building in other languages.
 
As you know, JavaScript does not have deterministic object destruction. The JavaScript garbage collector is fully automatic and asynchronous. Although PJSR has a gc() function that you can call to dispose of unreferenced objects, it works as a hint to the JavaScript engine, which is not necessarily executed when you perform the function call.

In the case of controls (Control and its derived objects), the situation is particularly complex regarding object destruction since controls typically generate many deep dependencies, including hierarchical parent-child object relations, sizer dependencies, and event handlers. This means that, in general, there is no way to force the disposal of a control object, which will generally happen asynchronously after its parent Dialog object is destroyed, with no guarantees during script execution.

My recommendation is to forget about this. Just generate as many controls as you need dynamically and use the delete keyword as appropriate to remove unused object properties. You can also assign null to unused properties to facilitate their garbage collection (make sure controls are not visible before deleting them or assigning null to their identifiers). Call gc( true/*exhaustive*/ ) occasionally to try to influence the garbage collector if you wish, although this usually won't work as you expect. Unless you create many thousands of complex controls in your script, there should be no practical problems. All unreferenced objects will be automatically deleted somewhere after your script terminates.
 
Thanks Juan for the detailed reply. I will code as you suggest. In my case I won’t be creating/removing 100s or 1000s of controls so the memory usage shouldn’t be a problem in my case.

Regards, Brad
 
Back
Top