Description
Hi, I'm trying to load a plugin (VST3 plugin in my case) from a dynamic library, and I'm running into some issues that I'd like to understand better ...
The general procedure (pseudo-ish code) goes like this:
// PSEUDO-CODE ...
let factory_ref: SmartPtrWrapper<PluginFactory> = unsafe {
let lib = libloading::Library::new(lib_path.as_path()).expect("plugin not found");
let func: libloading::Symbol<unsafe extern "C" fn() -> *mut PluginFactory> = lib
.get(b"GetPluginFactory")
.expect("plugin factory not found");
// call plugin factory closure
let ptr = func();
// on windows, I haven't found a workaround so far ...
#[cfg(target_os = "windows")]
mem::forget(lib);
SmartPtrWrapper::from_raw(ptr).unwrap()
};
On MacOS, that works just fine, but on both Linux and Windows, I get a segfault. On Linux, I have to load the library with RT_NODELETE
, then it works. On Windows, it crashes with a segfault unless I call mem::forget
.
I'm not sure what the implications are here, does that mean I'm intentionally leaking memory?
Or is the expectation that I keep a reference to the library as long as the pointer to GetPluginFactory
AND its returned results are in use?
I've read in some other issue (can't find it anymore) that something might be unmapped when the Library
instance is dropped, and I'm trying to understand better what that means ... any hints?
Best,
n