none
Exception thrown in a dynamically loaded Dll causes crash

    Question

  • Hello,

    I have set up a rough plugin system, that unloads the plugin's dll when my object's destructor is called.

    I face the following problem if an exception is raised inside the plugin's code:
    • the client code catches the exception
    • it leaves the try{} block, calling the plugin's destructor
    • the dll is unloaded
    • the access to the exception object in the catch{} causes the program to crash, since the exception was in the dll's code!

    The only workaround that I found is to pass a thrower function to the plugin's init code, and make the plugin call the thrower function on an exception, but I am not satisfied with this hack (it does not work if the exception is thrown by external code, for example)

    Is there a cleaner workaround?
    Friday, February 29, 2008 11:00 AM

Answers

  • You really should avoid throwing exceptions across a DLL boundary. Doing so creates all sorts of issues. You need to make sure that the DLL is compiled using exactly the same C++ compiler version and you need to ensure the same runtime library is used for both client and DLL.

     

    Moreover, throwing exceptions across a DLL boundary is completely unnecessary. If you were on my programmer team I'd send you back the whiteboard to come up with a better means of returning conditional information.

     

    Brian

     

     

    Friday, February 29, 2008 4:52 PM
  • If you create a plugin-architecture, you cannot afford to throw exceptions, because that way you tie the entire plugin stack to the same comopiler and compiler settings.

    You have to work with error codes or HRESULTs.

     

    Also be sure not to use DllMain for anything special, and do not have global C++ objects that are constructed at loadtime or destructed at unload time.

    These things will give you a world of hurt. Usually ouside of your development environment where everything works fine.

    Monday, March 03, 2008 11:31 AM

All replies

  • You really should avoid throwing exceptions across a DLL boundary. Doing so creates all sorts of issues. You need to make sure that the DLL is compiled using exactly the same C++ compiler version and you need to ensure the same runtime library is used for both client and DLL.

     

    Moreover, throwing exceptions across a DLL boundary is completely unnecessary. If you were on my programmer team I'd send you back the whiteboard to come up with a better means of returning conditional information.

     

    Brian

     

     

    Friday, February 29, 2008 4:52 PM
  • If you create a plugin-architecture, you cannot afford to throw exceptions, because that way you tie the entire plugin stack to the same comopiler and compiler settings.

    You have to work with error codes or HRESULTs.

     

    Also be sure not to use DllMain for anything special, and do not have global C++ objects that are constructed at loadtime or destructed at unload time.

    These things will give you a world of hurt. Usually ouside of your development environment where everything works fine.

    Monday, March 03, 2008 11:31 AM