Ada Reference Manual (Ada 2022) — Legal Information

# 13.11.2 Unchecked Storage Deallocation

1
Unchecked storage deallocation of an object designated by a value of an access type is achieved by a call to an instance of the generic procedure Unchecked_Deallocation.

#### Static Semantics

2
The following language-defined generic library procedure exists:
3/5
generic
type Object(<>) is limited private;
type Name is access Object;
procedure Ada.Unchecked_Deallocation(X : in out Name)
with Preelaborate, Nonblocking,
Global => in out Name'Storage_Pool,
Convention => Intrinsic;

#### Legality Rules

3.1/3
A call on an instance of Unchecked_Deallocation is illegal if the actual access type of the instance is a type for which the Storage_Size has been specified by a static expression with value zero or is defined by the language to be zero. In addition to the places where Legality Rules normally apply (see 12.3), this rule applies also in the private part of an instance of a generic unit.

#### Dynamic Semantics

4
Given an instance of Unchecked_Deallocation declared as follows:
5
procedure Free is
new Ada.Unchecked_Deallocation(
object_subtype_name, access_to_variable_subtype_name);
6
Procedure Free has the following effect:
7
1.
After executing Free(X), the value of X is null.
8
2.
Free(X), when X is already equal to null, has no effect.
9/5
3.
Free(X), when X is not equal to null first performs finalization of the object designated by X (and any coextensions of the object — see 3.10.2), as described in 7.6.1. It then deallocates the storage occupied by the object designated by X (and any coextensions). If the storage pool is a user-defined object, then the storage is deallocated by calling Deallocate as described in 13.11. There is one exception: if the object being freed contains tasks, it is unspecified whether the object is deallocated.
10/4
After the finalization step of Free(X), the object designated by X, and any subcomponents (and coextensions) thereof, no longer exist; their storage can be reused for other purposes.

#### Bounded (Run-Time) Errors

11
It is a bounded error to free a discriminated, unterminated task object. The possible consequences are:
12
No exception is raised.
13
Program_Error or Tasking_Error is raised at the point of the deallocation.
14
Program_Error or Tasking_Error is raised in the task the next time it references any of the discriminants.
15
In the first two cases, the storage for the discriminants (and for any enclosing object if it is designated by an access discriminant of the task) is not reclaimed prior to task termination.
15.1/4
An access value that designates a nonexistent object is called a dangling reference.
15.2/4
If a dangling reference is dereferenced (implicitly or explicitly), execution is erroneous (see below). If there is no explicit or implicit dereference, then it is a bounded error to evaluate an expression whose result is a dangling reference. If the error is detected, either Constraint_Error or Program_Error is raised. Otherwise, execution proceeds normally, but with the possibility that the access value designates some other existing object.

#### Erroneous Execution

16/3
Evaluating a name that denotes a nonexistent object, or a protected subprogram or subprogram renaming whose associated object (if any) is nonexistent, is erroneous. The execution of a call to an instance of Unchecked_Deallocation is erroneous if the object was created other than by an allocator for an access type whose pool is Name'Storage_Pool.

#### Implementation Advice

17
For a standard storage pool, Free should actually reclaim the storage.
17.1/3
A call on an instance of Unchecked_Deallocation with a nonnull access value should raise Program_Error if the actual access type of the instance is a type for which the Storage_Size has been specified to be zero or is defined by the language to be zero.
18
NOTE 1 The rules here that refer to Free apply to any instance of Unchecked_Deallocation.
19
NOTE 2 Unchecked_Deallocation cannot be instantiated for an access-to-constant type. This is implied by the rules of 12.5.4.

Ada 2005 and 2012 Editions sponsored in part by Ada-Europe