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::GetAssocAt(LPCTSTR 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
StanChartAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jaime OlivaresSoftware ArchitectCommented:
It is very strange if debugger stops before first line of function.
Sounds like you are pointing badly to a CMapStringToString object.
0
StanChartAuthor Commented:
"Sounds like you are pointing badly to a CMapStringToString object."

How can I be pointing badly to the object?  Pls explain.
0
Jaime OlivaresSoftware ArchitectCommented:
myMap->LookUp(...etcetera...)

If 'myMap' is not initialized, then would cause some unhandled exception.
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

StanChartAuthor Commented:
Hi Jaime,

My Map is initialized in the constructor of my object.  Furthermore, that map is accessed plenty of times before it crashes.

Cheers
0
Jaime OlivaresSoftware ArchitectCommented:
> 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.
0
StanChartAuthor Commented:
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
0
Jaime OlivaresSoftware ArchitectCommented:
>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
0
StanChartAuthor Commented:
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?

0
Jaime OlivaresSoftware ArchitectCommented:
> Do I have to create a new CMutex object for every member variable I want to be able to lock?
This will depend on how your information is organized and how many entities access it, and for what purposes. You can share a mutex for many member or even for the whole container object.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
StanChartAuthor Commented:
Cheers mate, all the points go to you.  I'll try to use the mutexes and see if I still have problems.

Thanks again.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
System Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.