It is currently Tue Mar 31, 2020 7:34 pm

All times are UTC + 1 hour




Post new topic Reply to topic  [ 6 posts ] 
Author Message
 Post subject: AtExit() for PureBasic
PostPosted: Tue Jun 28, 2016 12:25 am 
Offline
User
User

Joined: Mon Mar 20, 2006 8:44 am
Posts: 13
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:
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


Top
 Profile  
Reply with quote  
 Post subject: Re: AtExit() for PureBasic
PostPosted: Tue Jun 28, 2016 11:05 pm 
Offline
Addict
Addict
User avatar

Joined: Mon Oct 26, 2015 2:55 am
Posts: 913
Location: UA
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:
Procedure main ()
   ; main loop or all such stuff
   
   ; on_exit()
EndProcedure

main()
; on_exit_global()

End

_________________
Kind of copyleft notify: all the source code created by me and posted on Purebasic official forums is free to use and modification in all possible (and several impossible) ways for anyone, without asking my permission


Top
 Profile  
Reply with quote  
 Post subject: Re: AtExit() for PureBasic
PostPosted: Wed Jun 29, 2016 7:56 am 
Offline
Administrator
Administrator

Joined: Fri May 17, 2002 4:39 pm
Posts: 14011
Location: France
Just wondering, why do you need to patch the asm instead of using a custom function ? Does an external lib triggers 'End' ?


Top
 Profile  
Reply with quote  
 Post subject: Re: AtExit() for PureBasic
PostPosted: Thu Jun 30, 2016 7:55 am 
Offline
User
User

Joined: Mon Mar 20, 2006 8:44 am
Posts: 13
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.


Top
 Profile  
Reply with quote  
 Post subject: Re: AtExit() for PureBasic
PostPosted: Fri Jul 01, 2016 2:32 pm 
Offline
Enthusiast
Enthusiast
User avatar

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


Top
 Profile  
Reply with quote  
 Post subject: Re: AtExit() for PureBasic
PostPosted: Tue Nov 27, 2018 3:13 am 
Offline
Addict
Addict
User avatar

Joined: Wed Feb 17, 2010 12:00 am
Posts: 1324
Location: (Embarrassed to say country)
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.


Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 6 posts ] 

All times are UTC + 1 hour


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:
Jump to:  

 


Powered by phpBB © 2008 phpBB Group
subSilver+ theme by Canver Software, sponsor Sanal Modifiye