Hi all, Is using virtual destructors fine with a C++ plugin interface?
I have an example below that works. However i am not sure if it is a good practice. In particular is it ok to make use of virtual destructors in an application to destroy an object allocated in the plugin or should I define a pure virtual Destroy() method to ensure that the actual memory de-allocation (call to delete) is performed in the plugin where it was also initially allocated? Thanks, Brendon. Compile below using: g++ -o main -Wall -g -fPIC main.cpp -lltdl g++ -o myplugin.so -shared -Wall -g -fPIC myimpl.cpp ---- myiface.h ---- class MyIFace { public: virtual ~MyIFace() {} virtual void DoStuff() = 0; }; ---- myimpl.cpp ---- #include "myiface.h" #include <iostream> namespace { class MyImpl : public MyIFace { public: MyImpl() : blah("Hello") {} virtual void DoStuff() { std::cout << "DoStuff: " << blah << std::endl; } private: std::string blah; }; } // For GCC on UNIX #define MY_CALLTYPE __attribute__((cdecl)) #define MY_EXPORT __attribute__ ((visibility("default"))) extern "C" { MY_EXPORT void* MY_CALLTYPE MyFactory(void) { return static_cast<void*>(new MyImpl()); } } ---- main.cpp ---- #include "myiface.h" #include <iostream> #include <ltdl.h> typedef void* (*CreateFP)(void); int main() { lt_dlinit(); lt_dladdsearchdir("."); lt_dlhandle module = lt_dlopenext("myplugin"); CreateFP create_fp = (CreateFP)lt_dlsym(module, "MyFactory"); MyIFace* iface = static_cast<MyIFace*>(create_fp()); iface->DoStuff(); // Delete the instance!!!!!!!!!!! // IS THIS A GOOD IDEA? delete iface; iface = NULL; lt_dlclose(module); lt_dlexit(); return 0; } _______________________________________________ http://lists.gnu.org/mailman/listinfo/libtool