Question

When you have a base class in your main executable and subclasses (say, plugins) defined in DLLs, what happens when you want to get a plugin?

I am looking for an article/answer that'd clarify what happens when you

  • load a DLL
  • call a DLL's function that returns a plugin* (it has virtual functions)
  • use that plugin in your main executable's code
  • delete, unload

I am thinking about the vtable and other C++ issues. For instance, if you unload the DLL stil having some plugins running... The "code" will be gone?

Was it helpful?

Solution

Since you are talking about plugins, you must be doing something like LoadLibrary. Assuming Windows:

  • Load a library. You would normally call LoadLibrary followed by GetProcAddress. The DLL is loaded into the process address space and you have the pointer to the function exposed.
  • Call a DLL's function that returns a plugin* (it has virtual functions). You would cast a the return value from GetProcAddress to the function pointer type, and call it. Everything should work as normal.
  • Use that plugin in your main executable's code. There is nothing special.
  • Delete the plugin. I assume you use a function in the DLL to do so, and it should be fine. Directly calling delete can be dangerous, as the DLL may have a separate memory manager (depending on what runtime you use).
  • Unload the DLL. You call FreeLibrary, and the code is gone. The pointers you previously get from GetProcAddress become invalid.

I am not aware specific vtable issues. If you unload the DLL while the code is still running, I would assume the program will crash at this point, as the code address space becomes invalid.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top