Two memory-management functions are available for avoiding
memory leaks. The leaks slow your application and can cause the browser
to crash.
The issue arises primarily if you declare a widget or embedded
handler within a function. These in-function declarations allocate
memory at run time. Even after the function ends, the browser returns
the memory to the operating system only after the user has closed
the browser tab.
Each of the memory-management functions requests that the browser
return memory. The browser typically fulfills the request after a
delay, but while the application is still running.
The functions are as follows:
- UtilLib.destroyRUIHandler (ruiHandler Any in) requests
removal of the widgets in the specified handler, in any handlers embedded
in the specified handler, and in any handlers embedded in the embedded
handlers, to any level of depth. The input value must be a Rich UI
handler.
- UtilLib.destroyWidget (myWidget Widget in) requests
removal of the specified widget.
- UtilLib.destroyWidgetChildren (myWidget Widget in) requests
removal of the children of the specified widget
None of those functions returns a value.
If you are affected by the issue just described, consider these
coding conventions:
- Use the memory-management functions to return allocated memory
that is no longer needed.
- In a function, use anonymous declarations only if you have access
to the named container, which is the named widget that
embeds the anonymous declaration.
For example, consider a tooltip,
which is a box that is displayed when the user hovers over an area
on the page. A
tooltip provider is a function that returns
the box to be displayed, and the code for that function might be as
follows:
Function myProvider (widget any in) returns(Box)
return (new Box {children = [new Button{text = "Memory leak"}]});
end
In this example, the tooltip provider creates
a box that cannot be referenced from outside the function, and the
box itself includes an anonymous widget. The inability to reference
the box means that you cannot pass the box or its anonymous child
to a memory-management function.
To avoid the problem in this
example, declare the box outside the tooltip provider:
myBox Box{};
Function myProvider (widget any in) returns(Box)
myBox.children = [ new Button{text = "Problem solved"} ];
return(myBox);
end
Elsewhere in your application, you can now pass
myBox to
a memory-management function, as in the following example:
UtilLib.destroyWidgetChildren(myBox);
- After you use the removeChild or removeChildren function
to remove widgets from the DOM tree, remove the memory that was allocated
for those widgets. For details on those functions, see “Widget
properties and functions.”