I have a question regarding Shared Objects (or DLLs). I am working on a plugin loader for my game engine. I need one question answered before starting to do that, though.
If I load the data using an external function can I unload my shared object?
Here is a little code snippet:
//The Plugin:
class PythonScriptingPlugin : public Plugin {
void * system;
public:
PythonScriptingPlugin() {
system = new PythonScriptingSystem;
}
void * get() { return system; } //derived
};
extern "C" {
Plugin * libLoadPlugin() {
return new PythonScriptingPlugin;
}
}
//main app:
void * loadPlugin() {
void * handle = dlopen(Base::fs()->file("plugins", "libPythonScriptingPlugin.so"), RTLD_LAZY | RTLD_GLOBAL);
typedef (void*)(*loader)();
loader * libLoadPlugin = dlsym(handle, "libLoadPlugin");
void * data = libLoadPlugin()->get();
dlclose(handle);
return data;
}
Will data get dereferenced when I call dclose?
No, allocated memory remains, but you will not be able to call any function from that plugin/dll. That makes your object useless.