Where and When is Application (TApplication) instance created?
Asked Answered
R

2

10

Where and when is the Application instance created? (Same goes for the Screen instance) .

I don't see anything in the Forms or System initialization section.
In the CPU windows before Application.Initialize, I see a call to @_InitExe (SysInit) - which leads to _StartExe (System) and a whole lot of asm code - Which does not create the Application instance as far as I can tell.

What am I missing here?

Rab answered 7/2, 2013 at 17:32 Comment(0)
R
15

It's easy enough to work this out from the code. You just do a text search for TApplication.Create. For example using the IDE's Find in Files feature.

But you can always be lazy and get the debugger to do it.

  1. Enable Debug DCUs.
  2. Set a breakpoint in TApplication.Create.
  3. Run.

When the program breaks, look at the call stack. You will see that the TApplication object is instantiated from InitControls in the Controls unit. And InitControls is called from the initialization section of the Controls unit.

The full call stack for a plain vanilla VCL app looks like this:

Vcl.Forms.TApplication.Create(nil)
Vcl.Controls.InitControls
Vcl.Controls.Vcl.Controls
System.InitUnits
System._StartExe(???,???)
SysInit._InitExe($5A81BC)
Project1.Project1
:749933aa kernel32.BaseThreadInitThunk + 0x12
:76f09ef2 ntdll.RtlInitializeExceptionChain + 0x63
:76f09ec5 ntdll.RtlInitializeExceptionChain + 0x36

Doing the same thing with TScreen.Create, you will see that the TScreen object is also instantiated in InitControls().

I won't try and explain all of this. I think there's enough information and advice here for you to work it all out from here. Although this is the call stack from an XE3 application, it will look just the same for your Delphi 5 application.

Repute answered 7/2, 2013 at 17:37 Comment(2)
Makes me wonder why Application is declared in Forms unit in the first place (Controls uses Forms, Forms uses Controls)...Rab
Controls only uses Forms in implementation. TApplication has to be declared higher up than TForm since it refers to TForm in its interface section.Repute
M
12

The Application instance is created in the InitControls procedure of the Vcl.Controls.pas unit.

procedure InitControls;
begin
...
  Application := TApplication.Create(nil);
...

InitControls is called in the initialization section of the same unit:

initialization
  ...
  InitControls;
Magical answered 7/2, 2013 at 17:39 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.