Click here to Skip to main content
16,006,355 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: what about dll application Pin
Gérald Mercet25-Jan-02 3:14
Gérald Mercet25-Jan-02 3:14 
AnswerRe: what about dll application Pin
Michael P Butler25-Jan-02 0:26
Michael P Butler25-Jan-02 0:26 
GeneralSubClassing Pin
RaviRao24-Jan-02 20:05
RaviRao24-Jan-02 20:05 
Questionhow to change the IP address of local machine without rebooting? Pin
Shuyi Vi24-Jan-02 19:52
Shuyi Vi24-Jan-02 19:52 
GeneralDirectly accessing KB hardware Pin
Jason Hooper24-Jan-02 17:51
Jason Hooper24-Jan-02 17:51 
GeneralRe: Directly accessing KB hardware Pin
Nish Nishant24-Jan-02 19:45
sitebuilderNish Nishant24-Jan-02 19:45 
Questionstl/dll problem with virtual destructor? Pin
24-Jan-02 16:31
suss24-Jan-02 16:31 
AnswerRe: stl/dll problem with virtual destructor? Pin
Joaquín M López Muñoz24-Jan-02 21:22
Joaquín M López Muñoz24-Jan-02 21:22 
Hi Alexander, your problem does not have to do with STL, but rather is a consequence of how C++ handles virtual functions. Let me explain:
Suppose class A has one or more virtual functions (as the destructor in your case). Then, for A and every class deriving from A the compiler creates an static table of pointers to the actual implementations of such virtual member functions (what is called a virtual table). Then, a "hidden" pointer pvtable is placed inside each instance of A or a derived class pointing to the appropriate virtual table. This way, when a program calls a virtual function on a A* variable, it access the right implementation by means of pvtable (different compilers might use other approaches to implementing virtual member functions, but you got the idea).
Now, the problem with your app is that the main program is creating a virtual table for CSJImage and the DLL has created another CSJImage virtual table of its own. So, CSJImages created in the main program and in the DLL point to different (if equivalent) virtual tables. Now the cause of the crash is clear: when you unload the library, those CSJImages point to a no longer existent virtual table, and an access violation pops up when invoking their destructor.
Now for the solutions:
  • The simplest solution is of course avoiding releasing the library, or making the destructor non virtual.
  • If none of those are an option, then you have to make sure that all CSJImages are constructed inside the main program, not into the DLL. You can do so by defining a CSJImage factory function inside the main program and passing along a pointer to this factory to the DLL, like this:
    //main program
    typedef CSJImage* (* CSJIMAGE_FACTORY)();
    CSJImage* CSJImageFactory()
    {
      return new CSJImage();
    }
    typedef std::vector<CSJImage*> VectorType;
    typedef int(*fntTest)(VectorType*,CSJIMAGE_FACTORY);
    ...
    fntTest fntAddr;
    fntAddr=GetProcAddress(...);
    fntAddr(&vec,CSJImage); // we pass both the vector and the factory
    
    //DLL
    extern "C" __declspec(dllexport) 
    int Test(VectorType *vec,CSJIMAGE_FACTORY factory)
    {
      CSJImage* so1=factory();
      CSJImage* so2=factory();
      vec->push_back(so1);
      vec->push_back(so2);
      return 2;
    }
    The downside is that you have transformed your vectors of CSJImages into vectors of pointers to CSJImage, which adds the burden of properly deleting everything; but that's the price you have to pay for having polymorphic (i.e. with virtual member functions) types.

A rule of thumb when working with polymorphic types: whenever you define a polymorphic type A, have pointers to A instead of raw As. Otherwise, chances are things won't work as expected.
Hope this helps.

Joaquín M López Muñoz
Telefónica, Investigación y Desarrollo
GeneralRe: stl/dll problem with virtual destructor? Pin
25-Jan-02 3:28
suss25-Jan-02 3:28 
GeneralRTTI Help Pin
alex.barylski24-Jan-02 16:17
alex.barylski24-Jan-02 16:17 
GeneralRe: RTTI Help Pin
Christian Graus24-Jan-02 16:27
protectorChristian Graus24-Jan-02 16:27 
GeneralRe: RTTI Help Pin
alex.barylski24-Jan-02 18:00
alex.barylski24-Jan-02 18:00 
GeneralRe: RTTI Help Pin
Christian Graus24-Jan-02 18:08
protectorChristian Graus24-Jan-02 18:08 
Questionhow to show/hide the activate/deactive VIEW? Pin
AnonymousBabe@usa.net24-Jan-02 16:02
AnonymousBabe@usa.net24-Jan-02 16:02 
GeneralDesktop FLASHING?! Pin
gh24-Jan-02 16:01
gh24-Jan-02 16:01 
GeneralRe: Desktop FLASHING?! Pin
Christian Graus24-Jan-02 16:14
protectorChristian Graus24-Jan-02 16:14 
GeneralRe: Desktop FLASHING?! Pin
gh24-Jan-02 16:18
gh24-Jan-02 16:18 
GeneralRe: Desktop FLASHING?! Pin
Christian Graus24-Jan-02 16:28
protectorChristian Graus24-Jan-02 16:28 
GeneralRe: Desktop FLASHING?! Pin
gh24-Jan-02 16:31
gh24-Jan-02 16:31 
GeneralRe: Desktop FLASHING?! Pin
24-Jan-02 16:44
suss24-Jan-02 16:44 
GeneralRe: Desktop FLASHING?! Pin
gh24-Jan-02 16:51
gh24-Jan-02 16:51 
GeneralRe: Desktop FLASHING?! Pin
Nish Nishant24-Jan-02 17:31
sitebuilderNish Nishant24-Jan-02 17:31 
GeneralGUI Programming :: C++ Exclusive Pin
valikac24-Jan-02 13:29
valikac24-Jan-02 13:29 
GeneralRe: GUI Programming :: C++ Exclusive Pin
Christian Graus24-Jan-02 13:36
protectorChristian Graus24-Jan-02 13:36 
GeneralRe: GUI Programming :: C++ Exclusive Pin
Jon Sagara24-Jan-02 13:40
Jon Sagara24-Jan-02 13:40 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.