[RGSS3] Is there a way to do finalizers or dtors?

Usually I'd use;
  1. ObjectSpace.define_finalizer( self, proc { STUFF_TO_DO_HERE } )
Expand to see the code.

But RGSS3 doesn't have that.

I think the general idea is if you rely on finalizers in your RGSS then you are doing something wrong, but this sort of breaks down when you're using Win32 libraries.

Why I want a finalizer;
I've tried moving as much as I can to my DLL's DLL_PROCESS_DETACH call, but I'm doing some pretty crazy stuff that requires the RPG Maker Game.exe context to be alive for me to be able to clean up the memory I want to deal with and apparently the RGSS runtime will call the Win32API's FreeLibrary() AFTER it kills its own context (Wtf?).


So is there an RGSS class or point that I can attach to for my clean-up code to be called when the game is shutdown? Or is there a callback that I can attach?

EDIT:
I may have solved this myself:
  1. module SceneManager_XI
  2.  
  3.   def self.exit
  4.     STUFF_TO_DO_HERE # Cleanup library
  5.     super
  6.   end
  7.  
  8.   SceneManager.module_eval { include SceneManager_XI }
  9.  
  10. end
Expand to see the code.

Is this bad practise?

If it's a good idea and doesn't cause any problems I might write my own finalizer system that utilises this.
The big question is what happens under exceptional circumstances.

Seemingly trying to rescue SystemExit doesn't seem to work. Nor does ensure really ensure the code is run.
Neither of the following two examples work (nor with Exception => ex instead of SystemExit)
  1. begin
  2.   rgss_main { SceneManager.run }
  3. rescue SystemExit => ex
  4.   save_data("Exception: #{ex}", 'exception.txt')
  5. ensure
  6.   save_data("Save ensured", 'ensure.txt')
  7. end
Expand to see the code.


  1. rgss_main do
  2.   begin
  3.     SceneManager.run
  4.   rescue SystemExit => ex
  5.     save_data("Exception: #{ex}", 'exception.txt')
  6.   ensure
  7.     save_data("Save ensured", 'ensure.txt')
  8.   end
  9. end
Expand to see the code.


Alt+F4 does not trigger the saves. Exiting normally, and pressing F12 does.
If you store your Win32API objets in constants or global variables, the RGSS will never call the "free_function" of DL (Win32API) so the FreeLibrary won't be called until the RGSS kills itself.

By the way, the RGSS3 do have the ObjectSpace.define_finalizer but like the bitmap or sprites, it's not immedialty called when the objects are lost... unless you call GC.start.
Yeah no clean exit means my tidy up isn't called.

Windows doesn't call FreeLibrary when the host process crashes, does it? It just discards all the library memory in one go with the rest of the process memory. My problem is GPU objects being alive and needing the driver to clean up (Which isn't too much of a problem, crashing is exceptional behaviour)

Maybe I'm too worried
I don't know what Windows do.
I could make you try this :
Image
but it won't work with the RGSS3 that doesn't look like to raise an Exception when we press the close button so you can overwrite the WNDProc of the RGSS3 window :
  1.  
  2.     SetClassLong($HWND,GCL_WNDPROC,(long)Window_Manager::WndProc);
  3.     SetWindowLong($HWND,GWL_WNDPROC,(long)Window_Manager::WndProc);
  4.  
Expand to see the code.

Window_Manager::WndProc is my personnal WndProc and $HWND is the handle to the RGSSWindow.

In Ruby I get the HWND of the main window like this :
  1. def get_handle()
  2.     unless($HWND)
  3.       hwnd=FindWindowA.call("RGSS Player",nil);
  4.       spid="\x00\x00\x00\x00";
  5.       GetWindowThreadProcessId.call(hwnd,spid);
  6.       gpid=spid.unpack('i')[0];
  7.       pid=Process::pid();
  8.       if(gpid==pid)
  9.         $HWND=hwnd;
  10.       else
  11.         hwnd=GetForegroundWindow.call();
  12.         GetWindowThreadProcessId.call(hwnd,spid);
  13.         gpid=spid.unpack('i')[0];
  14.         if(gpid==pid)
  15.           $HWND=hwnd;
  16.         else
  17.           GetPrivateProfileString.call("Game","Title","",title="\x00"*256,256,".//Game.ini");
  18.           hwnd=FindWindowA.call("RGSS Player",title.delete("\x00"));
  19.           GetWindowThreadProcessId.call(hwnd,spid);
  20.           gpid=spid.unpack('i')[0];
  21.           if(gpid==pid)
  22.             $HWND=hwnd;
  23.           else
  24.             $HWND=0;
  25.             print("Impossible d'obtenir l'handle de la fenêtre de jeu...");
  26.           end
  27.         end
  28.       end
  29.     end
  30.     return $HWND
  31.   end
Expand to see the code.

You can do the same in C++, it's basicly calling the different Windows function that help me to get what I want :p

By overwriting the WndProc you'll lose the F1, F2 and Alt+Enter functions but you'll be able to receive the WM_CLOSE or WM_DESTROY event to make your clean up and exiting the RGSS by calling RGSSEval("exit(0)");

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=78471