To auto-create or not to auto-create

by Kent Reisdorph

Obviously a C++Builder GUI program consists of one or more forms. C++Builder gives you two options for runtime form creation; you can allow the VCL to auto-create your forms or you can take care of form creation yourself. By default, all new forms are auto-created.

Many C++Builder programmers have ignored the option of creating forms at runtime, blindly allowing the VCL to auto-create all forms. Why? Because auto-creation of forms is the IDE default and the alternative of runtime form creation never enters their minds. I should point out that when I talk about form creation options I am talking about all forms except the main form. The main form should always be auto-created.

So, of the two form creation options, which is better? To a degree, the answer to that question depends on the specific form being created. Auto-creation has one primary advantage—it’s easy. Very little thought is required to write an application in which all forms are auto-created. You simply build the form and access the form when needed:

AboutBox->ShowModal();

Auto-creation has two primary disadvantages. First, the application takes longer to load if there are several forms that need to be created. When a VCL application starts, it goes through the process of creating every form in the auto-create list. This takes time, especially if there are a lot of forms to create. The second disadvantage of auto-creation is that your application will use more memory than is required. If, for example, your application contains 20 forms, memory is allocated for every form, even when that form is not being displayed. If any of those forms allocate large blocks of memory in the constructor or OnCreate event handler, even more memory is being wasted.

Runtime creation of forms also has its advantages and disadvantages. For the most part, the advantages of runtime form creation are the inverse of the auto-creation disadvantages. That is, faster application loading and less memory used. One of the disadvantages of runtime creation is that a little more work is required by the programmer:

TAboutBox* about = new TAboutBox(this);
about->ShowModal();
delete about;

This is a simple example. The situation gets more complicated if you want your forms to maintain state information between invocations. Another disadvantage of runtime creation is there may be a slight delay while the form is being created. For simple forms this delay won’t be noticed, but for complex forms there may be a noticeable delay as the form is being created.

All things considered, I rarely use form auto-creation. To remove a form from the auto-create list, go to the Forms page of the Project Options dialog and move the form from the Auto-create list box to the Available forms list box. Don’t forget that C++Builder creates a global variable for all forms and that the variable will be NULL when the application starts if you are not using auto-creation. You can simply delete the global variable if it you are creating your forms at runtime.