Skip to content

Commit

Permalink
fix potential crash on exit in REAPER < 6.67 due to plugin registrati…
Browse files Browse the repository at this point in the history
…on keys being invalidated

cfillion/reapack#56
  • Loading branch information
cfillion committed Aug 18, 2022
1 parent 700e6ae commit fda35e5
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 21 deletions.
20 changes: 6 additions & 14 deletions src/api.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,27 +47,19 @@ API::~API()
knownFuncs().remove(this);
}

void API::RegInfo::announce() const
void API::RegInfo::announce(const bool add) const
{
// the original key string must remain valid even when unregistering
// in REAPER < 6.67 (see reapack#56)
if(value)
plugin_register(key.c_str(), value);
plugin_register(add ? key.c_str() : ("-" + key).c_str(), value);
}

void API::registerAll()
void API::announceAll(const bool add)
{
for(const API *func : knownFuncs()) {
for(const RegInfo &reg : func->m_regs)
reg.announce();
}
}

void API::unregisterAll()
{
for(const API *func : knownFuncs()) {
for(RegInfo reg : func->m_regs) {
reg.key.insert(reg.key.begin(), '-');
reg.announce();
}
reg.announce(add);
}
}

Expand Down
5 changes: 2 additions & 3 deletions src/api.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@

class API {
public:
static void registerAll();
static void unregisterAll();
static void announceAll(bool add);
static void handleError(const char *fnName, const reascript_error &);
static void handleError(const char *fnName, const imgui_error &);

Expand All @@ -36,7 +35,7 @@ class API {
struct RegInfo {
std::string key;
void *value;
void announce() const;
void announce(bool add) const;
} m_regs[3];
};

Expand Down
4 changes: 2 additions & 2 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
REAPER_PLUGIN_HINSTANCE instance, reaper_plugin_info_t *rec)
{
if(!rec) {
API::unregisterAll();
API::announceAll(false);
Resource::destroyAll(); // save context settings
return 0;
}
Expand All @@ -116,7 +116,7 @@ extern "C" REAPER_PLUGIN_DLL_EXPORT int REAPER_PLUGIN_ENTRYPOINT(
IMGUI_CHECKVERSION();

Window::s_instance = instance;
API::registerAll();
API::announceAll(true);

return 1;
}
4 changes: 2 additions & 2 deletions src/plugin_register.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class PluginRegister {

~PluginRegister()
{
m_key.insert(m_key.begin(), '-');
plugin_register(m_key.c_str(), m_value);
// the original m_key passed when registering must remain valid in REAPER < 6.67
plugin_register(("-" + m_key).c_str(), m_value);
}

private:
Expand Down

0 comments on commit fda35e5

Please sign in to comment.