Repository navigation
Destructor Module
Home › Procedures › Destructor (Module)
Specifies execution of a procedure at program termination
[Public | Private] Sub identifier [Alias "external_identifier"] [()] Destructor [priority] [Static]
{ procedure body }
End SubDefines 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 parameter list. A compile-time error will be generated 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:
The priority 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 attribute.
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 multiple modules are executed is known only at link time. Therefore, special care should be taken when using destructors that may call on a secondary module also defining a destructors. In such a case it is advisable to use a single destructor that explicit calls termination procedures in multiple modules to ensure a graceful termination 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 parameter list), of user defined Type can be defined as a module destructor, by adding the Constructor keyword used in the sub procedure definition.
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 the Identifier Rules page for the choice of user procedure identifier names (and specially the 'Platform Differences' paragraph).
- New to FreeBASIC
Sub pauseonexit Destructor
'' If the program reaches the end, or aborts with an error,
'' it will run this destructor before closing
Print "Press any key to end the program..."
Sleep
End Sub
Dim array(0 To 10, 0 To 10) As Integer
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)Category: Procedures
Kind: keyword
Reproduced from the FreeBASIC Wiki Manual under the GNU Free Documentation License 1.3 — with no Invariant Sections, no Front-Cover Texts and no Back-Cover Texts. Copyright © the FreeBASIC project and wiki contributors. See License.
tiko-lang · Home · Feature Index · Keyword Index · License
FreeBASIC manual pages reproduced under the GNU FDL 1.3.
- Generics
- Interfaces
- For Each
- Iterators
- Tuples
- Array Results
- Defer
- Lambdas
- Block Strings
- Partial Types
- String Slices
- Embed
- Typed Variadics
- C Header Import
- Foreign Library Linking
- UString
- Fixed-Width Types
- Nullptr
- String Library
- Format
- Array
- Map
- Set
- LinkedList
- OrderedMap
- OrderedSet
- Sorting
- Optional
- Result
- Smart Pointers
- Date and Time (Chrono)
- Filesystem
- File Streams
- Config (INI and TOML)
- Codec
- Hash
- UUID
- OS
- Process
- Net
- HTTP and TLS
- Math Library
- Finance
- Logging
- Terminal TUI Library
- PsControls
- tkControls
- sys/win
- Goroutines and Channels
- LLVM Backend
- tiko build
- TIKO_COMPILER
- Variable Declarations
- Standard Data Types
- User Defined Types
- Procedures
- Operators
- Control Flow Statements
- Converting Data Types
- Array Functions
- String Functions
- Mathematical Functions
- Bit Manipulation
- Date and Time Functions
- File I/O Functions
- Console Functions
- User Input (Runtime Library)
- User Input Functions (Graphics Library)
- Screen Functions
- 2D Drawing Functions
- Graphics Library Reference
- Graphics Keyword List
- Memory Functions
- Threading Support Functions
- Error Handling Functions
- Operating System Functions
- Preprocessor
- Intrinsic Defines
- Miscellaneous Keywords
- Modularizing