我可以确定我的单元初始化的顺序吗?
我正在寻找一个可能与单元初始化顺序有关的错误。有没有办法查看哪个初始化
部分何时执行?我需要知道顺序。这是在调试期间,所以我拥有 Delphi IDE 的全部功能,在我的例子中是 Delphi 2009。
我可以设置断点,但是当有很多单元时,这相当乏味。
您有什么建议吗?
I am hunting a bug which might be connected to unit initialization order. Is there a way to see which initialization
section was executed when? I need to know the order. This is during debugging, so I have the full power of the Delphi IDE, in my case Delphi 2009.
I could set breakpoints, but this is rather tedious when having many units.
Do you have any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
这是我刚刚在D2010中测试的一些代码,请注意,您需要在System.InitUnits中设置断点并获取InitContext var(@InitContext)的地址。然后修改 CtxPtr 以在仍在运行时具有此地址。 (也许有人知道一个更聪明的方法)。
/编辑:这里是使用 JclDebug 和映射文件的版本:
在我的情况下输出:
/编辑2:这里是 D2009 的版本(需要 JclDebug):
在我的系统上输出(Unitname.Unitname 实际上是 Unitname.Initialization):
Here is some code I just tested in D2010, note that you need to set a Breakpoint in System.InitUnits and get the address of InitContext var (@InitContext). Then modify CtxPtr to have this address WHILE STILL RUNNING. (Maybe someone knows a smarter way for this).
/EDIT: and here is a version using JclDebug and a mapfile:
Output in my case:
/EDIT2: And here a version for D2009 (requires JclDebug):
Output on my system (Unitname.Unitname is actually Unitname.Initialization):
请参阅在线帮助\程序和单元< /a> \ 初始化部分 和本文:了解 Delphi 单元初始化order
ICARUS 计算运行时初始化顺序其使用报告:
see Online Help \ Programs and Units \ The Initialization Section and this article: Understanding Delphi Unit initialization order
ICARUS computes the Runtime initialization order for its Uses Report:
您可以检查单元 System 和 SysInit 并查找过程 InitUnits。在这里您可以看到用 Delphi 编译的每个模块都有一个单元初始化和终止指针的列表。使用这些加上映射文件可能会给您准确的初始化顺序,但这需要一些指针黑客技术。
You might check out the unit System and SysInit and look for the procedure InitUnits. Here you see that every module compiled with Delphi has a list of units initialization and finalization pointers. Using those plus a map file might give you the exact initialization order, but it will take some pointer hackery.
怎么样?
添加到初始化部分
How about adding
to the initialization sections?
您可以在所有初始化部分设置断点,这些断点不会中断,但会向调试器日志写入消息。它将为您提供与添加
OutputDebugString('...')
调用相同的列表,但无需修改所有单元的源代码。You can set breakpoints on all initialization sections that don't break but write a message to the debugger log. It will give you the same list as adding
OutputDebugString('...')
calls but without having to modify the source code of all units.