Chapter 5. Finalization

5.1. Introduction
5.2. Finalization Reference
ext:set-finalizer — Associate a finalizer to an object.
ext:get-finalizer — Return finalizer associated to an object.

5.1. Introduction

As we all know, Common-Lisp relies on garbage collection for deleting unreachable objects. However, it makes no provision for the equivalent of a C++ Destructor function that should be called when the object is eliminated by the garbage collector. The equivalent of such methods in a garbage collected environment is normally called a finalizer.

ECL includes a simple implementation of finalizers which makes the following promises.

  • The finalizer can be any lisp function, let it be compiled or interpreter.

  • Finalizers are not invoked during garbage collection. Instead, if an unreachable object is found to have an associated finalizer, it is pushed into a list and before the next garbage collection cycle, the finalizer will be invoked.

  • If the finalizer is invoked and it makes the object reachable, for instance, by assigning it to a variable, it will not be destroyed, but it will have no longer a finalizer associated to it.

  • ECL will strive to call finalizers before the environment is closed and the program is finished, but this mechanism may fail when exiting in a non ordinary way.