ZetCode

Ada controlled types

last modified November 23, 2026

A controlled type runs code at the moments when its objects are created, copied, and destroyed. The hooks are ordinary primitive operations from Ada.Finalization, and the run-time calls them automatically. This article shows when Initialize, Adjust, and Finalize run, and builds a reference-counted handle from them.

Initialize and Finalize

The package declares Item as an extension of Ada.Finalization.Controlled. The two overriding procedures are the hooks: Initialize is called when an object is created without an explicit initial value, and Finalize when it goes out of scope.

tracked.ads
with Ada.Finalization;

package Tracked is
   type Item is new Ada.Finalization.Controlled with record
      Id : Positive := 1;
   end record;

   overriding procedure Initialize (I : in out Item);
   overriding procedure Finalize (I : in out Item);
end Tracked;

Item adds an Id component. The keyword overriding is required because both operations already exist in Controlled; a misspelt name would be rejected instead of silently declaring a new procedure.

tracked.adb
with Ada.Text_IO;

package body Tracked is
   use Ada.Text_IO;
   Next : Positive := 1;

   overriding procedure Initialize (I : in out Item) is
   begin
      I.Id := Next;
      Next := Next + 1;
      Put_Line ("created  " & Positive'Image (I.Id));
   end Initialize;

   overriding procedure Finalize (I : in out Item) is
   begin
      Put_Line ("destroyed" & Positive'Image (I.Id));
   end Finalize;

end Tracked;

Initialize assigns the next number and increments the package variable Next. Finalize prints the number of the object that is going away.

tracked_main.adb
with Ada.Text_IO;
with Tracked;
use Ada.Text_IO;

procedure Tracked_Main is
begin
   declare
      A : Tracked.Item;
      B : Tracked.Item;
   begin
      Put_Line ("inside, ids =" & Positive'Image (A.Id) & Positive'Image (B.Id));
   end;
   Put_Line ("after the block");
end Tracked_Main;

A and B live in an inner block. Initialize runs for A and then for B, so their ids are 1 and 2. At the end of the block, Finalize runs in the reverse order of creation: first B, then A. The last line runs after both objects are gone.

$ gnatmake -gnat2022 -gnatwa tracked_main.adb
$ ./tracked_main
created   1
created   2
inside, ids = 1 2
destroyed 2
destroyed 1
after the block

Adjust runs on assignment

A controlled object can be copied, and a plain bitwise copy is often wrong: two objects would share the same buffer or the same pointer. Adjust is called on the copy, after the bits have been copied, so that the new object can fix up what it shares with the source. A declaration with an initial value uses Adjust instead of Initialize; an assignment finalizes the old target and then adjusts the copied value.

copies.ads
with Ada.Finalization;

package Copies is
   type Ticket is new Ada.Finalization.Controlled with record
      Serial : Natural := 0;
   end record;

   overriding procedure Initialize (T : in out Ticket);
   overriding procedure Adjust (T : in out Ticket);
   overriding procedure Finalize (T : in out Ticket);
end Copies;

Adjust takes its parameter in out: it sees the copied value and is allowed to change it. All three operations are declared in the specification so that they override the inherited ones.

copies.adb
with Ada.Text_IO;

package body Copies is
   use Ada.Text_IO;
   Next : Natural := 1;

   overriding procedure Initialize (T : in out Ticket) is
   begin
      T.Serial := Next;
      Next := Next + 1;
      Put_Line ("initialize" & Natural'Image (T.Serial));
   end Initialize;

   overriding procedure Adjust (T : in out Ticket) is
   begin
      Put_Line ("adjust" & Natural'Image (T.Serial));
   end Adjust;

   overriding procedure Finalize (T : in out Ticket) is
   begin
      Put_Line ("finalize" & Natural'Image (T.Serial));
   end Finalize;

end Copies;

Initialize numbers a fresh object, while Adjust and Finalize only report the serial number they see. That is enough to show the order in which the hooks run.

copies_main.adb
with Ada.Text_IO;
with Copies;
use Ada.Text_IO;

procedure Copies_Main is
   A : Copies.Ticket;
   B : Copies.Ticket := A;
begin
   Put_Line ("serials" & Natural'Image (A.Serial) & Natural'Image (B.Serial));
   B := A;
   Put_Line ("after the assignment");
end Copies_Main;

B : Ticket := A is initialized from A, so only Adjust runs and B receives a copy of serial 1. The assignment B := A finalizes the old value of B and then adjusts the copied value, which is why the output shows finalize 1 followed by adjust 1. A real type would use Adjust to duplicate a buffer instead of sharing it. The two finalizations at the end destroy B and then A.

$ gnatmake -gnat2022 -gnatwa copies_main.adb
$ ./copies_main
initialize 1
adjust 1
serials 1 1
finalize 1
adjust 1
after the assignment
finalize 1
finalize 1

Limited controlled types

Limited_Controlled has the same hooks but forbids copying and assignment. A lock, an open file, or any resource that must have exactly one owner belongs in a limited controlled type: it still gets Initialize and Finalize, but the compiler rejects every attempt to duplicate it.

locks.ads
with Ada.Finalization;

package Locks is
   type Lock is new Ada.Finalization.Limited_Controlled with record
      Key : Positive := 1;
   end record;
   overriding procedure Initialize (L : in out Lock);
   overriding procedure Finalize (L : in out Lock);
end Locks;

The type extends Limited_Controlled and keeps a key. The body prints when the lock is acquired and released, so finalization is visible even though the program below never gets to run.

locks.adb
with Ada.Text_IO;

package body Locks is

   overriding procedure Initialize (L : in out Lock) is
   begin
      Ada.Text_IO.Put_Line ("acquired" & Positive'Image (L.Key));
   end Initialize;

   overriding procedure Finalize (L : in out Lock) is
   begin
      Ada.Text_IO.Put_Line ("released" & Positive'Image (L.Key));
   end Finalize;

end Locks;

The declaration of B is fine on its own, but the assignment B := A is not: a limited type has no assignment operation.

lock_main.adb
with Locks;

procedure Lock_Main is
   A : Locks.Lock;
   B : Locks.Lock;
begin
   B := A;
end Lock_Main;

The compiler reports that the left-hand side may not be limited. Removing the assignment and giving each lock its own scope is the usual fix; the Finalize hook still runs when each scope ends.

$ gnatmake -gnat2022 -gnatwa lock_main.adb
x86_64-linux-gnu-gcc-14 -c -gnat2022 -gnatwa lock_main.adb
lock_main.adb:7:04: error: left hand of assignment must not be limited type
gnatmake: "lock_main.adb" compilation error

An applied example: a shared reading

The last example counts references to a shared cell. Initialize allocates the cell with one reference, Adjust increments the count when a handle is copied, and Finalize decrements it and frees the cell when the last handle disappears. This is the usual reason to implement all three operations together.

readings.ads
with Ada.Finalization;

package Readings is
   type Cell is record
      Value : Integer := 0;
      Refs  : Natural := 0;
   end record;

   type Cell_Access is access Cell;

   type Handle is new Ada.Finalization.Controlled with record
      Data : Cell_Access := null;
   end record;

   overriding procedure Initialize (H : in out Handle);
   overriding procedure Adjust (H : in out Handle);
   overriding procedure Finalize (H : in out Handle);
end Readings;

Cell is the payload and Handle is the controlled type. Data is visible in the record, so the example can print the value and the count without accessor functions.

readings.adb
with Ada.Unchecked_Deallocation;

package body Readings is
   procedure Free is new Ada.Unchecked_Deallocation (Cell, Cell_Access);

   overriding procedure Initialize (H : in out Handle) is
   begin
      H.Data := new Cell'(Value => 0, Refs => 1);
   end Initialize;

   overriding procedure Adjust (H : in out Handle) is
   begin
      H.Data.Refs := H.Data.Refs + 1;
   end Adjust;

   overriding procedure Finalize (H : in out Handle) is
   begin
      H.Data.Refs := H.Data.Refs - 1;
      if H.Data.Refs = 0 then
         Free (H.Data);
      end if;
   end Finalize;

end Readings;

Free is an instance of Unchecked_Deallocation for the cell type. Adjust is called with Data already pointing at the shared cell, so it only increments the count. Finalize must not free the cell while other handles still refer to it.

readings_main.adb
with Ada.Text_IO;
with Readings;
use Ada.Text_IO;

procedure Readings_Main is
begin
   declare
      First  : Readings.Handle;
      Second : Readings.Handle;
   begin
      First.Data.Value := 21;
      Second := First;
      Second.Data.Value := 42;
      Put_Line ("first  =" & Integer'Image (First.Data.Value));
      Put_Line ("second =" & Integer'Image (Second.Data.Value));
      Put_Line ("refs   =" & Natural'Image (Second.Data.Refs));
   end;
   Put_Line ("cell released");
end Readings_Main;

First is created and gets a cell. Second is created with a cell of its own, but the assignment Second := First finalizes that cell and adjusts the copied handle, so both handles point at the first cell with two references. Writing through Second is therefore visible through First. When the inner block ends, both handles are finalized; the second one finds the count at zero and frees the cell.

$ gnatmake -gnat2022 -gnatwa readings_main.adb
$ ./readings_main
first  = 42
second = 42
refs   = 2
cell released

Next steps

The next article defines operators such as "+" and "=" for a type of your own. Operators are ordinary functions, so they follow the same visibility and primitive rules as the procedures in this article.

Source

Ada Reference Manual

GNAT User's Guide

AdaCore learning resources

In this article we have derived types from Ada.Finalization.Controlled and Limited_Controlled, watched Initialize and Finalize run at the ends of a scope, used Adjust for assignment, rejected a copy of a limited object, and built a reference-counted handle. The tagged types article explains the type extension syntax that controlled types use.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Ada tutorials.