
std::map.find() crashing in DLL
I'm seeing a big problem that I'm trying to fully understand in order
to fix my current problem and avoid similar issues in the future. In
brief it is this:
I have an object 'foo' implmented in my client (exe) which holds an
std::map<long,std::string>. The map is populated with entries at
construction. The constructor takes a string (a comma-separated
key-value set), parses the string, fills the map via the [] operator
-- foo also holds onto the original string in a member variable:
// statically linked into the exe
class Foo {
Foo(std::string&); // parses the string and fills the map
Foo(foo const &); // gets the original string from the parameter
// and fills the map
~Foo();
std::string GetValueFromKey(long); // return value from map for
given key
std::map<long,std::string> myMap;
std::string originalString;
Quote:
};
In DLL #1, I create an instance of foo with a string and pass a
reference to that foo object into another object (bar) created and
owned by another DLL, but to which I have a pointer:
Foo aFoo("1=A,2=B,3=C");
pBar->SomeFunc(aFoo);
In DLL #2, the Bar object has a function which uses map.find():
Bar::SomeFunc(Foo& inFoo) {
std::string value = inFoo->GetValueFromKey(1);
Quote:
}
The call to map.find() causes a memory exception. I need to know why.
I've found a work-around, but it involves shuffling around where I
create the foo object and how I pass it around. I assume the problem
is due to the fact that I'm passing a reference created in one DLL
into another DLL, but why?
It appears as though the map is getting FUBARed, but the std::string
member variable remains valid?
At first, I thought that perhaps I was using multiple heaps, but in my
project settings, all my DLLs and the client EXE are set to "Debug
Multithreaded DLL" and "Multithreaded DLL" for debug and release,
respectively. So I don't *think* that's the problem.
Anyone know what's going on here?