Better is
https://www.freepascal.org/docs-html/rtl/system/addexitproc.html{$mode objfpc}
program demoexit;
procedure helloexit;
begin
writeln('Going to the exit');
end;
begin
AddExitProc(@helloexit);
end.
The reason is that this keeps the exitproc
chain intact, whereas the previous example does not.
That can cause issues.
The last added in the chain is executed last.
@Fibonacci
You destroyed that chain. Although it is a "working" example it is wrong.
There may be frameworks that rely on that chain to release resources or otherwise.
The correct way is as per my example.
I know that the documentation lacks a bit here, both the wiki - which is plain wrong - and the real docs.
It comes from TurboPascal:
"Compared to the simpler ExitProc, AddExitProc automatically preserves the existing chain, ensuring that previously registered exit procedures are not overwritten. This is particularly useful in larger applications or frameworks that rely on proper resource cleanup."