Finalizers
A finalizer is a special method-like type member that executes once, just before a Class or Record gets destroyed as part of Garbage Collection or Automatic Reference Counting.
Finalizers are meant as a last resort to clean up resources (such as unmanaged resources on .NET and Java), in case the type was not properly closed or disposed of as it should have been.
Note: Finalizers (like Constructors and Custom Operators) are very similar in structure to regular methods, and many topics covered in the Methods topic will apply to them as well, in particular the section on Method Body.
Similar to Constructors, Oxygene uses a special finalizer
keyword to declare finalizers. Finalizers are always considered private
, and they cannot be called explicitly from code, they will only be called implicitly, by the runtime. Finalizers cannot have parameters or a return type.
type
MyClass = public class
finalizer;
begin
...
end;
end;
On .NET and Java, finalizers will be executed on a special thread run by the Garbage Collector instead of the main thread.
On .NET, Java and Island, you should make sure to understand using
Statements and the IDisposable/AutoCloable
pattern before deciding whether your classes require a finalizer. Read More:
Platform Considerations
Finalizers for Records are supported only on the Cocoa and Island platforms. On .NET and Java, finalizers are restricted to Classes.
Visibility
Finalizers always have private
visibility, regardless of what Visibility Section they are declared in. Explicit Visibility Modifiers other than private
are not allowed on finalizers.
Virtuality
Finalizers do not directly participate in Polymorphism, and cannot have Virtuality Modifiers. If a base class implements a finalizer, the compiler will automatically make sure finalizers in descendant classes will safely call the base finalizer as last step of their execution.
Other Modifiers
No modifiers are allowed on finalizers.
See Also
- Disposable Pattern
IDisposable
Pattern (.NET)Object.Finalize
Method (.NET)- Understanding when to use a Finalizer in your .NET class (.NET)
AutoCloable
and how to use it (Java)IDisposable
(Island)