Freeing Windows Resources on the fly in Delphi


Here is a fix for the out of resources problem many of us have 
experienced when using Delphi to create a program with a large 
number of controls (several hundred).

To illustrate the problem, I created a test program (RSTEST1) 
with four forms.  Each form has a panel and one or two buttons.  
Forms 2 and 3 also have a TTabSet and a TNotebook while form 4 
has a TTabbedNotebook.  The notebooks and tabbed notebook  each 
have 4 pages, one blank and the others with many controls.

If you run the program and cycle through all the forms and tabs 
while running a resource monitoring program such as Dr. Bob's 
RESMON available in the libraries, you will see a large 
percentage of your resources being consumed.

I wrote a unit, FREERES.PAS which contains a procedure named 
FreeResources which takes a TWinControl as its handle.  When 
called, it causes the VCL to free the resources used by that 
control and its descendants.  Note that if the controls were 
visible they will disappear.  When you cause them to be shown 
again, the resources will be reallocated by the VCL.

The second sample program, RESTEST2, has several examples of 
using FreeResources on forms, notebook pages, and tabbed notebook 
pages.  Form 3 frees the resources after another page or form has 
been displayed, while the other forms do the reverse:  free the 
resources of the visible page or form then show the new one.  
This latter method causes more of a flicker on slow machines 
while the former method temporarily consumes more resources.  
Take your pick.

One thing you need to watch out for, it only works for controls 
which never receive messages when they are not visible.  I guess 
if someone used TWinControl or TCustomControl to derive a, for 
example, Winsock component which does not really need to be 
visible but needs a windows handle, you could run into problems.  
I suggest testing it thoroughly in your application before 
delivering it to paying customers!  Thanks to  Don Hatlestad for 
pointing this out.

I hope this helps, and I welcome any feedback either through the 
Delphi forum on Compuserve by E-mail:  Compuserve 76012,3614 or 
Internet jseach@ix.netcom.com.

Jim Seach
