# DLLs & Static Data

This topic is 3669 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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?

##### Share on other sites
Quote:
 Original post by DaveFollowing 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.

##### Share on other sites
Yes that is how i understand it. Why does it sound suspicious?

##### Share on other sites
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.

##### Share on other sites
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?

##### Share on other sites
No, you just put the static data in the DLL.

##### Share on other sites
Hmm, i thought i tried that. Ok i will have another look.

Thanks,

1. 1
2. 2
Rutin
21
3. 3
4. 4
A4L
15
5. 5

• 13
• 26
• 10
• 11
• 9
• ### Forum Statistics

• Total Topics
633737
• Total Posts
3013607
×