Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where and When is Application (TApplication) instance created?

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?

like image 556
kobik Avatar asked Feb 07 '13 17:02

kobik


2 Answers

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.

like image 152
David Heffernan Avatar answered Nov 08 '22 14:11

David Heffernan


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;
like image 34
jachguate Avatar answered Nov 08 '22 15:11

jachguate