On this page
B6005: memory might leak
Catalog status: active. Default severity: error. Stable name: memory-leak. Family: Ownership and finalization.
Verada proves that a program frees every object it allocates, given the contracts of the library and of imported code. An object that owns access values (see ownership) must own nothing, that is, every access value in it must be null or moved out, wherever it:
- dies: at the end of its scope, or when the subprogram returns;
- is overwritten: by an assignment, or as the actual of an
outparameter; - is freed by an instance of
Ada.Unchecked_Deallocation: the freed object itself must own nothing, or what it owns would be lost.
The prover found a path on which an owned object would be lost.
Example (Vada)
procedure Clear (L : in out List) is
Rest : List;
begin
if L /= null then
Rest := L.Next;
Free (L);
end if;
end Clear; -- Rest still owns the rest of the listExample (Varc)
fn clear(l: inout List) {
var rest: List;
if l != null {
rest = l.next;
free(&l);
}
} // rest still owns the rest of the list
A build without proof has no run-time check for leaks: only the proof shows there are none (see modes).
What to do (Vada)
Free what the object owns, or move it to an owner that outlives it (an out parameter, a result, a component of a longer-lived object): here, Clear (Rest); after Free (L);.
What to do (Varc)
Free what the object owns, or move it to an owner that outlives it (an out parameter, a result, a component of a longer-lived object): here, clear(&rest); after free(&l);.
Regression source and further reading
Complete regression example: source, command arguments and expected results. The fixture deliberately reproduces this diagnostic.
See ownership and finalization, the code index, and diagnostic interpretation for modes, replay, fix-its and JSON.