我在Project(exe)的A类中创建一个CMapStringToString。我在A类中填充了这个地图,但是我需要将这个地图发送到不同Project(dll)中的B类。碰巧的是,每当我尝试查找密钥时,我都会遇到异常。
然而,当我尝试在同一个A级中查找密钥时,它的工作正常。
//This class is in exe project.
void A::AMethod
{
CMapStringToString my_map; //This map is a member variable.
my_map.SetAt(_T("Key"), _T("value"));
B obj; //This class is in a DLL
obj.BsMethod(&my_map); //This will crash later in class B.
//And apparently I cannot pass by value
//(which is bad but yeah).
---------------------------------------------------------------------------
CString sKey, sValue;
sKey.Format(_T("key"), sValue);
CMapStringToString* pTempCheckMap;
pTempCheckMap = &my_map;
pTempCheckMap->LookUp(sKey, sValue); //This works fine and I get the value.
----------------------------------------------------------------------------
};
//This class is in different project, a DLL and the method is exported.
void B::BMethod(CMapStringToString* myPopulatedMap)
{
if(NULL != myPopulatedMap) //Map has valid reference and is populated.
{
CString sKey, sVal;
sTemp.Format(_T("key"));
myPopulatedMap->LookUp((LPCTSTR)sKey, sVal); //Crashes here.
}
}
我尝试进一步调试LookUp,在GetAssocAt方法中Map_ss.cpp发生了崩溃。以下是摘录。
CMapStringToString::CAssoc*
CMapStringToString::GetAssocAt(LPCTSTR key, UINT& nHash) const
// find association (or return NULL)
{
nHash = HashKey(key) % m_nHashTableSize;
if (m_pHashTable == NULL)
return NULL;
// see if it exists
CAssoc* pAssoc;
for (pAssoc = m_pHashTable[nHash]; pAssoc != NULL; pAssoc = pAssoc->pNext)
{
if (pAssoc->key == key)
return pAssoc;
}
return NULL;
}
对此方面的任何见解都将受到高度赞赏。 感谢您的期待。