?
Solved

Program crashing when looking up value in CMapStringToString class

Posted on 2005-03-30
10
Medium Priority
?
710 Views
Last Modified: 2013-11-20
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
Comment
Question by:StanChart
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 5
10 Comments
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13662844
It is very strange if debugger stops before first line of function.
Sounds like you are pointing badly to a CMapStringToString object.
0
 

Author Comment

by:StanChart
ID: 13662949
"Sounds like you are pointing badly to a CMapStringToString object."

How can I be pointing badly to the object?  Pls explain.
0
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13663017
myMap->LookUp(...etcetera...)

If 'myMap' is not initialized, then would cause some unhandled exception.
0
Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

 

Author Comment

by:StanChart
ID: 13663450
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13663503
> 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
 

Author Comment

by:StanChart
ID: 13663691
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
 
LVL 55

Expert Comment

by:Jaime Olivares
ID: 13663804
>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
 

Author Comment

by:StanChart
ID: 13663918
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
 
LVL 55

Accepted Solution

by:
Jaime Olivares earned 2000 total points
ID: 13663977
> 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
 

Author Comment

by:StanChart
ID: 13664028
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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month11 days, 14 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question