Recent

Author Topic: is atexit() availabel for fpc 3.2.2 Application's ?  (Read 444 times)

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
is atexit() availabel for fpc 3.2.2 Application's ?
« on: May 23, 2025, 11:03:39 am »
Hi,
is there a Windows win32api Function that act like atexit() from the LibC-Library ?
So, when Halt() is called, the atexit set Routine is called.
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Fibonacci

  • Hero Member
  • *****
  • Posts: 754
  • Internal Error Hunter

paule32

  • Hero Member
  • *****
  • Posts: 516
  • One in all. But, not all in one.
Re: is atexit() availabel for fpc 3.2.2 Application's ?
« Reply #2 on: May 23, 2025, 11:12:32 am »
Thanks fibonacci.
never see, not yet testet a procedure end;
MS-IIS - Internet Information Server, Apache, PHP/HTML/CSS, MinGW-32/64 MSys2 GNU C/C++ 13 (-stdc++20), FPC 3.2.2
A Friend in need, is a Friend indeed.

Fibonacci

  • Hero Member
  • *****
  • Posts: 754
  • Internal Error Hunter
Re: is atexit() availabel for fpc 3.2.2 Application's ?
« Reply #3 on: May 23, 2025, 11:15:18 am »
Code: Pascal  [Select][+][-]
  1. program atexit;
  2.  
  3. procedure x;
  4. begin
  5.   writeln('xxxx');
  6.   readln;
  7. end;
  8.  
  9. begin
  10.   ExitProc := @x;
  11.   writeln('1');
  12.   Halt;
  13.   writeln('2');
  14.   readln;
  15. end.

Thaddy

  • Hero Member
  • *****
  • Posts: 17176
  • Ceterum censeo Trump esse delendam
Re: is atexit() availabel for fpc 3.2.2 Application's ?
« Reply #4 on: May 23, 2025, 11:22:19 am »
Better is
https://www.freepascal.org/docs-html/rtl/system/addexitproc.html
Code: Pascal  [Select][+][-]
  1. {$mode objfpc}
  2. program demoexit;
  3. procedure helloexit;
  4. begin
  5.   writeln('Going to the exit');
  6. end;
  7.  
  8. begin
  9.   AddExitProc(@helloexit);
  10. 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."
« Last Edit: May 23, 2025, 11:41:02 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Thaddy

  • Hero Member
  • *****
  • Posts: 17176
  • Ceterum censeo Trump esse delendam
Re: is atexit() availabel for fpc 3.2.2 Application's ?
« Reply #5 on: May 23, 2025, 11:42:18 am »
Ergo: the TP docs are better here.
When used correctly it is very powerful.
« Last Edit: May 23, 2025, 11:43:52 am by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

Fibonacci

  • Hero Member
  • *****
  • Posts: 754
  • Internal Error Hunter
Re: is atexit() availabel for fpc 3.2.2 Application's ?
« Reply #6 on: May 23, 2025, 11:52:02 am »
Nah its not wrong. There is no chain if you dont create one, nothing is destroyed. You should know Paule is coding weird stuff, right? In this case Paule wants the ExitProc, trust me, I know the context.

Thaddy

  • Hero Member
  • *****
  • Posts: 17176
  • Ceterum censeo Trump esse delendam
Re: is atexit() availabel for fpc 3.2.2 Application's ?
« Reply #7 on: May 23, 2025, 12:13:29 pm »
Yes, he is trying to code way beyond his current capabilities. Which is admirable, but needs corrections.
In my opinion it is wrong to directly assign to exitproc, though.
For the reasons I mentioned.

I will correct the wiki entry this weekend. It is mindblowingly wrong.
« Last Edit: May 23, 2025, 01:41:20 pm by Thaddy »
Due to censorship, I changed this to "Nelly the Elephant". Keeps the message clear.

 

TinyPortal © 2005-2018