Page 1 of 1

AtExit() for PureBasic

Posted: Tue Jun 28, 2016 12:25 am
by ward
My new project needs atexit() function, but purebasic don't have it.
Here is my way to resolve this problem: using inline asm and FASM macro directive.

Code: Select all

Declare AtExitHandler()
Global NewList atexit_list.i()
Global exit_code.i, *exit_handler = @AtExitHandler()

Procedure AtExit(*callback)
  AddElement(atexit_list())
  atexit_list() = *callback
EndProcedure

Procedure AtExitHandler()
  ForEach atexit_list()
    CallFunctionFast(atexit_list())
  Next
EndProcedure

!macro _PB_EndFunctions nul
!{
   !label _PB_EndFunctions
   EnableASM
     mov eax, dword [esp+4]
     mov exit_code, eax
     call *exit_handler
   DisableASM
!}

!macro PB_EndFunctions nul
!{
  !label PB_EndFunctions
  EnableASM
    mov rax, [PB_ExitCode]
    mov exit_code, rax
    sub rsp, 40
    call *exit_handler
    add rsp, 40
  DisableASM
!}

Procedure Test1()
  MessageRequester("AtExit1", "exit code: " + Str(exit_code))
EndProcedure

Procedure Test2()
  MessageRequester("AtExit2", "exit code: " + Str(exit_code))
EndProcedure

AtExit(@Test1())
AtExit(@Test2())

End 123

Re: AtExit() for PureBasic

Posted: Tue Jun 28, 2016 11:05 pm
by Lunasole
Interesting ^^ But don't know when it might be needed, usually when I need to do something on shutdown I'm adding calls like following:

Code: Select all

Procedure main ()
	; main loop or all such stuff
	
	; on_exit()
EndProcedure

main()
; on_exit_global()

End

Re: AtExit() for PureBasic

Posted: Wed Jun 29, 2016 7:56 am
by Fred
Just wondering, why do you need to patch the asm instead of using a custom function ? Does an external lib triggers 'End' ?

Re: AtExit() for PureBasic

Posted: Thu Jun 30, 2016 7:55 am
by ward
I like to write library as include file (*.pbi) rather than binary library file.
So I need a way to ensure some resources will be deleted correctly before program end.

Every program language can add on_exit() procedure after main part, but they still have "atexit" feature.
This means it is sometimes useful.

Re: AtExit() for PureBasic

Posted: Fri Jul 01, 2016 2:32 pm
by PMV
As long as you are only using your own functions, you can create your
own "system" that will provide a functionality to do cleanup on exit. But
this will always create a dependancy on this custom solution. On bigger
projects, or when you have multiple projects, every project will have this
dependancy. Additional, if you are using 3th-party code, this will
mostly have its own solution that you need to care about.
And you have to be carefull with this as you need to handle errors
additional to catch all possible endings.

If PB would have the possitility to register a callback-function, that is always
be called when a program ends, it would make propper cleaning easier. :)

Just my 2 cent.

MFG PMV

Re: AtExit() for PureBasic

Posted: Tue Nov 27, 2018 3:13 am
by jassing
ward wrote:My new project needs atexit() function, but purebasic don't have it.
Here is my way to resolve this problem: using inline asm and FASM macro directive.
Old thread -- but wanted to say "thanks" for this -- I am working on adding a module to a larger program, and don't have access to the full source, so I can't change everything -- I needed to run some code on exit to ensure a database is closed properly -- this works perfectly (win10 x64) Thank you. Wish this were a built-in feature.