DLL and STL

Started by
3 comments, last by Simplicity 18 years, 3 months ago
I ran into this avoiding problem with DLL and STL. Consider: DLL1: class A { std::map< int, int > Map; }; A* Create() { //This is psuedo, assumes it dynamically loads DLL2 and get the function // pointer to the CreateB function declared in DLL2 (CREATE*)pfn = LoadDLL(DLL2)-GetSymbol("CreateB"); return( pfn() ); } DLL2: class B : public A { void Init() { Map[0] = 50; } }; A* CreateB() { return( new B() ); } DLL2 has a class derived from DLL1 and link at compile time. EXE: //Link to DLL1 at compile time A a = Create(); //Create from DLL1 //This cause some weird heap error that tooked several hours to figured out. delete a; The problem was that std::map< int, int > Map didn't carry over properly or something. If I remove that from class A and place it in class B, everything is ok. How to solve this problem?
Advertisement
You shouldnt create something in DLL B and delete it in your exe. Preferably you should make a Destroy() function which will delete the object in the DLL it was created in.

Also, ensure that both DLLs and the exe make use of a dynamic C/C++ runtime.
Ok so I make the proper Create and Destroy functions from the DLL. I still cannot properly derive from the class within a different DLL.
You can probably solve the problem if you make every function in the base class virtual and never use the constructors between DLLs...

(and suddenly, you begin to re-invent COM)
Found it:

http://support.microsoft.com/kb/q168958/

This topic is closed to new replies.

Advertisement