Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 734
  • Last Modified:

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
0
StanChart
Asked:
StanChart
  • 5
  • 5
1 Solution
 
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

  • 5
  • 5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now