The finalization section is
optional and can appear only in units that have an initialization section. The
finalization section begins with the reserved word finalization and
continues until the end of the unit. It contains statements that are executed
when the main program terminates. Use the finalization section to free
resources that are allocated in the initialization section.
Finalization sections are executed
in the opposite order from initializations. For example, if your application
initializes units A, B, and C, in that order, it will finalize them in the
order C, B, and A.
Once a unit’s initialization code starts to execute, the corresponding finalization section is guaranteed to execute when the application shuts down. The finalization section must therefore be able to handle incompletely initialized data, since, if a runtime error occurs, the initialization code might not execute completely.
Program structure and syntax:
Overview
Unit structure and syntax: Overview
Unit references and
the uses clause
Multiple and indirect
unit references
Unit structure and syntax: Overview
结束节是可选的,当且仅当单元中有初始化节时才允许有结束节。结束节以保留字finalization开始,直到单元结束。当主程序终止时,结束节中的语句被执行。在初始化节中分配的资源,通常在结束节中释放。
对于项目中的多个单元,结束节执行的顺序与初始化节执行的顺序相反。例如,如果在应用程序启动时按照单元A、B、C的顺序进行初始化,那么在应用程序结束时将按照单元C、B、A的顺序执行相应的结束节。
一旦单元初始化节的代码开始执行,则相应的结束节将在应用程序结束时保证被执行。所以结束节必需能完全处理初始化过的数据,因为当运行时错误发生时,初始化节代码可能没有完全执行。
编者注
结束节中的语句在程序结束时执行一次,程序运行中不执行。运行中,其它语句也无法将执行点跳转到结束节中。