Browse Source

Call optional destroy() callback before unloading plugin library.

tags/v2.0.0
Andrew Belt 3 years ago
parent
commit
9c70d94977
2 changed files with 27 additions and 5 deletions
  1. +12
    -4
      include/plugin/callbacks.hpp
  2. +15
    -1
      src/plugin.cpp

+ 12
- 4
include/plugin/callbacks.hpp View File

@@ -2,9 +2,17 @@
#include <plugin/Plugin.hpp> #include <plugin/Plugin.hpp>




/** Called once to initialize and return the Plugin instance.
You must implement this in your plugin
/** Called immediately after loading your plugin.

Use this to save `plugin` to a global variable and add Models to it.
Required in plugins.
*/ */
extern "C" {
extern "C"
void init(rack::plugin::Plugin* plugin); void init(rack::plugin::Plugin* plugin);
}

/** Called before your plugin library is unloaded.

Optional in plugins.
*/
extern "C"
void destroy();

+ 15
- 1
src/plugin.cpp View File

@@ -247,8 +247,22 @@ void init() {


void destroy() { void destroy() {
for (Plugin* plugin : plugins) { for (Plugin* plugin : plugins) {
// We must delete the plugin *before* freeing the library, because the vtable of Model subclasses are static in the plugin, and we need it for the virtual destructor.
void* handle = plugin->handle; void* handle = plugin->handle;

// Call destroy() if defined in the plugin library
typedef void (*DestroyCallback)();
DestroyCallback destroyCallback = NULL;
if (handle) {
#if defined ARCH_WIN
destroyCallback = (DestroyCallback) GetProcAddress((HMODULE) handle, "destroy");
#else
destroyCallback = (DestroyCallback) dlsym(handle, "destroy");
#endif
}
if (destroyCallback)
destroyCallback();

// We must delete the Plugin instance *before* freeing the library, because the vtables of Model subclasses are defined in the library, which are needed in the Plugin destructor.
delete plugin; delete plugin;


// Free library handle // Free library handle


Loading…
Cancel
Save