AtExit() for PureBasic

Windows specific forum
ward
User
User
Posts: 13
Joined: Mon Mar 20, 2006 8:44 am

AtExit() for PureBasic

Post 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
User avatar
Lunasole
Addict
Addict
Posts: 1091
Joined: Mon Oct 26, 2015 2:55 am
Location: UA
Contact:

Re: AtExit() for PureBasic

Post 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
"W̷i̷s̷h̷i̷n̷g o̷n a s̷t̷a̷r"
Fred
Administrator
Administrator
Posts: 16681
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: AtExit() for PureBasic

Post by Fred »

Just wondering, why do you need to patch the asm instead of using a custom function ? Does an external lib triggers 'End' ?
ward
User
User
Posts: 13
Joined: Mon Mar 20, 2006 8:44 am

Re: AtExit() for PureBasic

Post 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.
PMV
Enthusiast
Enthusiast
Posts: 727
Joined: Sat Feb 24, 2007 3:15 pm
Location: Germany

Re: AtExit() for PureBasic

Post 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
jassing
Addict
Addict
Posts: 1774
Joined: Wed Feb 17, 2010 12:00 am

Re: AtExit() for PureBasic

Post 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.
Post Reply