|
Using std::map Across DLL Boundaries
Submitted by |
Note: This problem only occurs with the Microsoft STL implementation!
I don't know how many of you have run into this problem, but it certainly
annoyed the heck out of me. The scenario was this:
I was writing a plugin/extension system and used a STL map for my plugin
registry, basically mapping classnames to factories which can create that class,
i.e. pretty standard stuff. One or several plugin classes could be stored in a
DLL and my system traverses a user-defined directory and adds all the plugins
contained in DLLs to its registry. Every DLL had a single entry function which
would register the DLLs' content with the plugin system.
Now, I thought I could just pass the STL map to the DLL and be done with it, but
that seemed to be a pretty bad idea ... it didn't work ... the program crashed
when it tried to touch the map which I passed to the DLL. Or rather, it crashed
when I tried to insert something into the map. Now, at first I thought I was
doing something dumb, but when I switched to STLPort (another STL
implementation) the problem vanished. In other words, there had to be something
wrong with the Microsoft STL implementation. It turns out, that the code for the
tree implementation (which is used to represent the map) uses references to
static variables ... this is not so nice, since the actual values of these
differ from executable to DLL. Therefore you couldn't just use an
executable-side map in a DLL. Now, one solution to this is to switch to STLPort
which you can find here:
http://www.stlport.org/
This library is really nice, but not everyone might have the chance to migrate
to another STL implementation (for God knows what reasons).
A search on the web turned out the following site:
http://www.dinkumware.com/vc_fixes.html
There you can find some bugfixes for the VC++ header files. The file 'xtree' is
the one you need to replace (always make backups!!!) in your VC++ include
directory in order to be able to use std::map across DLL boundaries. It also
fixes some multithreading issues with the xtree-derived STL classes.
Hope this tip helps somebody,
Marco Koegler (MK42)
|
The zip file viewer built into the Developer Toolbox made use
of the zlib library, as well as the zlibdll source additions.
|