Mic*_*nny 3 delphi initialization
I had an issue where a file kept deleting on startup and I couldn't track down the code responsible. I wound up adding Vcl.Dialogs to all the units and creating an initialization section that looked like this:
initialization
begin
ShowMessage('Inside [Unit Name Here]');
end;
Run Code Online (Sandbox Code Playgroud)
This was quite a pain. Is there an easy way to generate a list of forms/units in the order in which they fire off?
UPDATE: 2019-08-01 (Helpful MAP links)
Here are two links that may assist in understanding DELPHI map files
您确实不需要费心修改源单元。我想您会发现,使用以下方法比起以某种方式生成单元列表然后进行耕作的方法,查找行为异常的单元要快得多。
如果查看System.Pas,您将找到一个类似InitUnits的过程(来自D7)。
procedure InitUnits;
var
Count, I: Integer;
Table: PUnitEntryTable;
P: Pointer;
begin
if InitContext.InitTable = nil then
exit;
Count := InitContext.InitTable^.UnitCount;
I := 0;
Table := InitContext.InitTable^.UnitInfo;
[...]
try
while I < Count do
begin
P := Table^[I].Init;
Inc(I);
InitContext.InitCount := I;
if Assigned(P) then
begin
TProc(P)();
end;
end;
except
FinalizeUnits;
raise;
end;
end;
Run Code Online (Sandbox Code Playgroud)
这是导致每个单元的初始化代码被调用的代码。它遍历各个单元,并通过调用来调用每个单元的初始化部分(如果有)
TProc(P)();
Run Code Online (Sandbox Code Playgroud)
您可以在循环之前检查Count的值。即使对于一个相对简单的项目,它的成百上千个也不要感到惊讶。
在TProc(P)()上设置一个断点;行并单击鼠标右键,然后将PassCount设置为Count值的一半。运行您的应用程序,并在断点跳闸时检查文件是否已删除。
然后,您可以对Count的值进行二进制搜索(如果文件仍然存在,则继续当前运行,或者重置应用程序并将Pass Count减半),以准确确定导致文件删除的单位。因为您可以使用二进制搜索来执行此操作,所以它将快速收敛到要删除文件的单元上。当然,当断点跳闸时,您可以通过按TProc(P)()上的F7来跟踪单元的初始化代码(如果已使用调试信息进行编译);