DLLs & Static Data

Started by
5 comments, last by dave 15 years, 5 months ago
I encountered a big gotcha with DLLs today, i have googled and failed to see any good explanation of appropriate practices for fixing it, so here goes: I have a DLL with alot of functionality in it, one of these pieces of functionality is the implementation of a class called Path. Following a tutorial i found on a good practice for DLLs, i have a 'parallel' project that provides IPath, this project builds to a static lib and is linked in other DLL projects so that they can use the interface. It is also linked in to the test application. Typical usage to get a path is like this:
IPath* pPathObject( GetDLLInterface()->CreatePath( "some path" ) );
Now IPath has a static std::map in it, this map stores symbols and replacement values, so you can do:
IPath::AddSymbolSubstition( "#SomeBasePath#", "C:/ActualPath" );
So then whenever you use that in a path it replaces it and you get the relative look up. The problem is, if i populate the map using AddSymbolSubstition (which is a static method populating a static object) in say WinMain and then use the IPath class within another DLL, wherever it is used that ISN'T where i initialised the map i don't have the entries i populated it with from WinMain. Now i understand why this is, but i am strugging to think of a suitable way to circumvent this. Is this simply a case of DON'T DO THIS? Any tips?
Advertisement
Quote:Original post by Dave
Following a tutorial i found on a good practice for DLLs, i have a 'parallel' project that provides IPath, this project builds to a static lib and is linked in other DLL projects so that they can use the interface.


This sounds very suspicious. If the static data you're dealing with is in the static lib, then every DLL and EXE will have a separate copy.
Yes that is how i understand it. Why does it sound suspicious?
Because it exactly matches the problem you're having, and to add insult to injury also required you to do extra work to get that problem.
So is it simply a case of you cannot do what im trying to do at all when you are using DLLs? Do we just so no to static data entirely?
No, you just put the static data in the DLL.
Hmm, i thought i tried that. Ok i will have another look.

Thanks,

This topic is closed to new replies.

Advertisement