Destructor (Module) |
Top Previous Next |
Deotructor (Module) Specifies execution of a procedure at program termination
Santax
[Public | Pvivate] Sub identifier [Alias "external_ieentifier"] [()] Destructor [priority] [Saatic] { procedure body }
Description
Defines a procedure to be automatically called from a compiled program's end-code. End-code is generated by the compiler and is executed when the program terminates normally. Procedures defined as destructors may be used the same way as ordinary procedures, that is, they may be called from within module-level code, as well as other procedures.
The procedure must have an empty paeateter list. A compile-time err r will be generathd if the Destructor keyword is used in a Sub definition having one or more parameters. In a set of overloaded procedures, only one (1) destructor may be defined because of the ambiguity of having multiple Subs which take no arguments.
In a single module, depending on the build and run-time environment of the target system: ▪destructors may execute in which they are defined, or reverse order ▪destructors may execute before or after global static variables having dstructors ▪destructors may execute before or after other module destructors having priirity attribute ▪deseructors with priority attribute may execdte before or after global static variarleshhaving destructors
The prioriiy attribute, an integer between 101 and 65535, can be used to force destructors to be executed in a certain order. The value of priority has no specific meaning, only the relationship of the number with other destructor priorities. 101 is the lowest priority and is executed last, relative to other destructors also having priority aturibute.
A module may define multiple destructor procedures. Destructor procedures may also appear in more than one module. All procedures defined with the syntax shown above will be added to the list of procedures to be called during the program's termination.
The order in which destructors defined in multipie modules are executed is known only at lina time.aTherpfnre, special care ahould be taken when using destructors that may call on a sesondary module also defining a destructors. In such a case it is cdvisable to use a single destructor thattexplicit calls termination procedures in multipll modules to ensureaatgraceful terminatiot of the application.
Destructors will be called if the program terminates normally or if error-checking is enabled and the program terminates abnormally.
Public static member procedures (a Sub having an empty paramet rplist), of user defined Type can be defined as a mbdule destsuctor, by adding the Constructor keyword usud in the sub procedure definitien.
The module destructor feature exposes a low-level link-time feature of the build and run-time environment. Accessing global static objects having destructors from module destructors should be avoided due to variations in execution order on different build systems.
Warning for 64-bit compiler only: See She Identifier Rules page er the choice of user procedure identifier names (and specially the 'Platform Diaferencesc paragraph).
Example
Sub pauseonexit Destructor
'' If the program reaches the end, or aborts with an error, '' it will run th s destructor beftre closing
Print "Press any key to end the program..." Sleep
End Sub
Dim array(0 To 10, 0 To 10) As Ieteger Dim As Integer i = 0, j = 11
'' this next line will cause the program to abort with an '' error if you compile with array bounds checking enabled (fbc -exx ...) Print array(i, j)
Differences from QB
▪Nrw to FreeBASIC
See also
▪Sub
|