Go Back   Cockos Incorporated Forums > REAPER Forums > REAPER Bug Reports

Reply
 
Thread Tools Display Modes
Old 10-24-2020, 11:54 PM   #1
pandabot
Human being with feelings
 
pandabot's Avatar
 
Join Date: Oct 2018
Posts: 290
Default [NOT A BUG] reaper.atExit function is not run when you close Reaper project

I expected Reaper to close any currently running scripts as if a user pressed the X button so the cleanup can be done, but this doesn't seem to be happening. For example I save dock state using the atexit function, but this dock state is not being saved unless the user closes the script window manually first

From the docs:
Code:
Lua: reaper.atexit(function)

Adds code to be executed when the script finishes or is ended by the user.

Last edited by pandabot; 10-25-2020 at 12:03 AM.
pandabot is offline   Reply With Quote
Old 10-25-2020, 04:05 PM   #2
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 1,610
Default

No, you must code that yourself. The script can still run, even if the gui window is closed. That way you can have scripts, who show a gui only under certain conditions while still running all the time.

You need to have something like this in your code for the atexit to have an effect:

Code:
Key=gfx.getkey()
if Key~=-1 then reaper.defer(functionname) end -- this keeps the script running for as long as the window is opened
You probably have reaper.defer already in your script. Just alter it in your script like in the above example.
Exchange "functionname" with the function that's already used by your defer-function.
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1294 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is online now   Reply With Quote
Old 10-25-2020, 05:15 PM   #3
nofish
Human being with feelings
 
nofish's Avatar
 
Join Date: Oct 2007
Location: home is where the heart is
Posts: 10,028
Default

This is probably not of much help, but your report is surprising to me because I remember one case where I had written a script for a user and he noticed when quitting Reaper with that script still running it produced an error message. Turned out it was because of an SWS function call I put in atExit() unaware that SWS may get unloaded before that call is done.
Anyway that seemed to indicate to me that atExit() does get called when closing Reaper (never investigated myself though).

Here's the thread:
https://forum.cockos.com/showthread....56#post2224756
nofish is offline   Reply With Quote
Old 10-25-2020, 05:22 PM   #4
cfillion
Human being with feelings
 
cfillion's Avatar
 
Join Date: May 2015
Location: Québec, Canada
Posts: 3,281
Default

The callback is invoked when quitting REAPER here (tried with 5.99 and 6.15):

Code:
function l() reaper.defer(l); end
reaper.defer(l)

reaper.atexit(function()
  reaper.ShowMessageBox('foo', 'bar', 0)
end)
cfillion is offline   Reply With Quote
Old 10-25-2020, 05:27 PM   #5
Meo-Ada Mespotine
Human being with feelings
 
Meo-Ada Mespotine's Avatar
 
Join Date: Apr 2020
Location: Leipzig
Posts: 1,610
Default

Ok, I'm a little confused.

@pandabot
Which x-button are you referring to? The one of the gui-window of the script or the one of Reaper's main window?
__________________
Use you/she/her, when contacting me, please. Thanks :) Not mentoring via PMs, sorry.
Ultraschall-API - 1294 ReaScript functions for Reaper - Reaper Internals - Developerdocs4Reaper - Donate, if you wish
Meo-Ada Mespotine is online now   Reply With Quote
Old 10-25-2020, 05:59 PM   #6
pandabot
Human being with feelings
 
pandabot's Avatar
 
Join Date: Oct 2018
Posts: 290
Default

I've since updated my script to not use atexit, but I think I know what was going on. What I was doing was docking the window in a particular place and then quitting Reaper. I expected the script to be docked in the same place when I ran it again from that project.

I saved project state in the atexit call using reaper.SetProjExtState, but I don't think this state is saved unless the project is also saved. So I guess the atexit call was running but the effect of reaper.SetProjExtState was not saved. I bet it would have worked if I followed it up with reaper.Main_SaveProject

@cfillion Thanks for testing that, you're right it is being run. I don't know why I thought it was behaving differently when closing manually, must have been saving the project without thinking. I was a little bleary eyed doing all this stuff last night

Last edited by pandabot; 10-25-2020 at 06:06 PM.
pandabot is offline   Reply With Quote
Old 10-27-2020, 07:13 AM   #7
Buy One
Human being with feelings
 
Join Date: Sep 2019
Posts: 334
Default

Quote:
Originally Posted by pandabot View Post
but I don't think this state is saved unless the project is also saved
indeed it's not
Buy One is offline   Reply With Quote
Old 10-28-2020, 03:38 PM   #8
lb0
Human being with feelings
 
Join Date: Apr 2014
Posts: 3,721
Default

Quote:
Originally Posted by pandabot View Post
I bet it would have worked if I followed it up with reaper.Main_SaveProject
That would depend on the order things happen when Reaper is quitting. If the project gets closed before the scripts are unloaded/terminated (which I think from memory may be the case) - then calling Main_SaveProject won't work as there is no project to save when the script atexit function is called.

But things may have changed since I last looked into this issue - or work differently in your case - although it sounds very similar to a problem I've never managed to solve with one of my scripts. I had a workaround in that I could trigger the script to save the necessary data when saving the project manually - so had to always remember to hit Ctrl+S before shutting down Reaper.

https://forum.cockos.com/showthread.php?t=215415
__________________
Projects - Reascripts - Lua:
LBX Stripper | LBX Chaos Engine | LBX Floating FX Positioner | LBX SRD Smart Knobs
Donate via Paypal
lb0 is offline   Reply With Quote
Old 10-28-2020, 04:06 PM   #9
pandabot
Human being with feelings
 
pandabot's Avatar
 
Join Date: Oct 2018
Posts: 290
Default

Just to clarify I was only speaking rhetorically about calling reaper.Main_SaveProject, it's probably not good practice for a script to be saving the project for the user. I often go into a project to check things out, maybe move some settings around and then exit without saving. It would be pretty disastrous for me if scripts were saving projects without me knowing
pandabot is offline   Reply With Quote
Old 10-29-2020, 01:56 AM   #10
lb0
Human being with feelings
 
Join Date: Apr 2014
Posts: 3,721
Default

Quote:
Originally Posted by pandabot View Post
Just to clarify I was only speaking rhetorically about calling reaper.Main_SaveProject, it's probably not good practice for a script to be saving the project for the user. I often go into a project to check things out, maybe move some settings around and then exit without saving. It would be pretty disastrous for me if scripts were saving projects without me knowing
Yes - I agree - saving projects within a script needs to be transparent to the user.

I know you already have a solution to your issue - I was just pointing out (to anyone reading this) that I don't think adding a SaveProject in the atexit would work in the case of it being triggered when Reaper is closing.
__________________
Projects - Reascripts - Lua:
LBX Stripper | LBX Chaos Engine | LBX Floating FX Positioner | LBX SRD Smart Knobs
Donate via Paypal
lb0 is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:57 PM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2020, vBulletin Solutions Inc.