StanChart
asked on
Program crashing when looking up value in CMapStringToString class
Hi,
I'm having some problems when looking up a value in my CMapStringToString map. My application receives updates from an external application at which point I look up the update in my map. Sometimes, I get an unhandled exception in the Lookup method of the CMapStringToString class. This exception is very hard to replicate but I just caught it in the debugger in the file MAP_SS.cpp:
CMapStringToString::CAssoc *
CMapStringToString::GetAss ocAt(LPCTS TR key, UINT& nHash) const
// find association (or return NULL)
{ <------------------------- ---------- ---------- ---------- ---------- -----This is where it stops execution.
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;
}
My key is a valid key and I pass it in as a CString so I was thinking that there might be a problem converting from CString to LPCTSTR.
Any help would be appreciated.
Thanks much
I'm having some problems when looking up a value in my CMapStringToString map. My application receives updates from an external application at which point I look up the update in my map. Sometimes, I get an unhandled exception in the Lookup method of the CMapStringToString class. This exception is very hard to replicate but I just caught it in the debugger in the file MAP_SS.cpp:
CMapStringToString::CAssoc
CMapStringToString::GetAss
// 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;
}
My key is a valid key and I pass it in as a CString so I was thinking that there might be a problem converting from CString to LPCTSTR.
Any help would be appreciated.
Thanks much
ASKER
"Sounds like you are pointing badly to a CMapStringToString object."
How can I be pointing badly to the object? Pls explain.
How can I be pointing badly to the object? Pls explain.
myMap->LookUp(...etcetera. ..)
If 'myMap' is not initialized, then would cause some unhandled exception.
If 'myMap' is not initialized, then would cause some unhandled exception.
ASKER
Hi Jaime,
My Map is initialized in the constructor of my object. Furthermore, that map is accessed plenty of times before it crashes.
Cheers
My Map is initialized in the constructor of my object. Furthermore, that map is accessed plenty of times before it crashes.
Cheers
> My application receives updates from an external application at which point I look up the update in my map
Have you some concurrent access?
Maybe 2 processes trying to access the same object?
Sometimes this causes unpredictable errors.
Have you some concurrent access?
Maybe 2 processes trying to access the same object?
Sometimes this causes unpredictable errors.
ASKER
I thought of this but I don't think this is the cause because the other classes that would access this map are not always created when the crash occurs. How would I be able to prevent this? I remember reading about using Mutexes to lock the memory but I don't know how to use them. Perhaps I am receiving two updates from the other application at the same time and this is causing the crash?
I appreciate your help on this because I know it's not easy to figure out what's going on without seeing the code and being there to debug it when it happens.
Cheers
I appreciate your help on this because I know it's not easy to figure out what's going on without seeing the code and being there to debug it when it happens.
Cheers
>Perhaps I am receiving two updates from the other application at the same time and this is causing the crash?
Yes, this is a possibility.
>I appreciate your help on this because I know it's not easy to figure out what's going on without seeing the code and being there to debug it when it happens.
You are one of the few EE members that are aware of this.
About the mutex, just have to define a CMutex object (let's say m_Mutex) in the same place you have defined the CMap object.
Then, when you want to access the map object, lock the mutex this way:
CSingleLock lock(&m_Mutex);
singleLock.Lock(); // Attempt to lock the shared resource
if (singleLock.IsLocked()) // Resource has been locked
{
//...use your map object here
singleLock.Unlock();
}
But you must pay attention to used the mutex in every process you want to access the map
Yes, this is a possibility.
>I appreciate your help on this because I know it's not easy to figure out what's going on without seeing the code and being there to debug it when it happens.
You are one of the few EE members that are aware of this.
About the mutex, just have to define a CMutex object (let's say m_Mutex) in the same place you have defined the CMap object.
Then, when you want to access the map object, lock the mutex this way:
CSingleLock lock(&m_Mutex);
singleLock.Lock(); // Attempt to lock the shared resource
if (singleLock.IsLocked()) // Resource has been locked
{
//...use your map object here
singleLock.Unlock();
}
But you must pay attention to used the mutex in every process you want to access the map
ASKER
Thanks for your reply and help with using the mutexes.
One last question:
Do I have to create a new CMutex object for every member variable I want to be able to lock? i.e, If I have 10 maps, 3 booleans and 2 CStrings, should I create 15 mutexes?
One last question:
Do I have to create a new CMutex object for every member variable I want to be able to lock? i.e, If I have 10 maps, 3 booleans and 2 CStrings, should I create 15 mutexes?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Cheers mate, all the points go to you. I'll try to use the mutexes and see if I still have problems.
Thanks again.
Thanks again.
Sounds like you are pointing badly to a CMapStringToString object.