Allow C ++ Builder / Delphi to create TForms at startup or create manually?

When TForm is created in Builder (or Delphi), the code is added to main to create these forms when the application starts:

 Application->CreateForm( __classid( TxForm), &xForm ); 

this simplifies the job, but is it reasonable when applications have 10, 20, 50, 100 forms? I guess this can capture all kinds of memory and system pens. A form can always be created on the fly if necessary, deleting it using the "Project-> Parameters-> Forms" dialog box and:

 std::auto_ptr< TxForm > myForm( new TxForm( this )); myForm->ShowModal(); 

So the question is, is it better for C ++ Builder to do this in its own way or manually create a form if necessary?

+6
delphi vcl c ++ builder
source share
5 answers

Forms created, but not yet visible, take memory and make run time, but should not use any system descriptors, because things like window handles are not allocated until the window is shown.

With that said, I almost always create forms manually (using code similar to your auto_ptr ), because I cannot have global variables. Manually creating forms provides several advantages: faster launch of the application, forms are always displayed in a known good condition and it is easier to switch later to show several instances of the form at once if you need to.

As the VCL documentation explains , delete inputting a form directly (or indirectly, using the std::auto_ptr margin) can cause an error memory, since the form may have unprocessed messages. To avoid this problem, I wrote two classes of smart pointers that take care of calling Release instead of delete . You can download them from http://gist.github.com/671452 .

+2
source share

It depends on how you use forms in your application. I usually create them on the fly when necessary, and then release them when I finish. This is more manual but saves resources (and costs a little CPU each time, which can cause delays).

If you rarely use the form, I would say create / free (delete) manually when necessary, but if you use the form all the time and all your life in the application, stay with the default method.

+5
source share

Assuming that the IDE "does it in its own way" means that the IDE uses some kind of intelligence to create code to create forms and data modules. This is not true. It is simply the creation of what you told him to create, and in the order in which you told him. If you have not explicitly indicated what to do using the Project Settings dialog box, then it simply creates things in the order in which you added them to your project. The IDE knows nothing better than you.

Using automatically generated forms and data modules encourages the use of global variables that the IDE declares so you can reference these objects. They don’t need you.

Auto-create only your main form. When you need something else, create it yourself using the typical way to create objects: call the constructor and store references to objects in local variables or fields of other objects of yours, and not on global global variables that you must delete. Do not worry CreateForm . I wrote an article explaining why.

+5
source share

IMO, forms of automatic creation that you probably won’t use, such as the About box, are just wasteful. Usually I automatically create the main form, and then dynamically create the rest, as needed.

+2
source share

If the application is not just a VERY SMALL or POC test layer, create only the main form (and / or global datamodule, if it is your style / design) and create all the others manually.

Creating all forms at boot time is just waste and (depending on what is done on the forms) can make your program a quick load in the sea turtle walking in the sand ...

0
source share

All Articles