473,322 Members | 1,431 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Standard Template Library map:find() returns garbage

I have run into a problem that I cannot seem to figure out regarding a method call to STL map::find(). When the key value can't be found in the map (this is an expected scenario), it returns end() that has _MyValue = unitialized memory (it appears). Thus, the returned iterator is garbage and an exception is thrown eventually. I thought that find() returned the location succeeding the last element in the map if a match was not found. It seems to be returning end(). The map looks like this:

Expand|Select|Wrap|Line Numbers
  1. typedef list< CpProts > SRCP_PROTS_LIST;
  2. typedef map< long, SRCP_PROTS_LIST*, CpProtsCmp, CpProtsList2 >    SRCP_PROTS_LISTMAP;
where:
- CpProts is a class containing methods and fields
- class CpProtsCmp : public less< long > {};
- class CpProtsList : public allocator< CpProts > {};
- class CpProtsList2 : public allocator< std::list<CpProts,CpProtsList> *> {};

here is the code where the exception occurs:
Expand|Select|Wrap|Line Numbers
  1. DWORD imageType = 0x1000007F;
  2. SRCP_PROTS_LISTMAP m_ProtsListMap;
  3. SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
  4. SRCP_PROTS_LIST* pList = (*listMapIt).second;  <<-- 
  5.  
  6. listMapIt.ptr->second =  0xbaadf00d 
a little complicated but it has worked up until we changed from VS 6.0 to VS 2005 earlier this year. I've inspected the map once all of the values are added and everything looks like I would expect. Using a debug build, I've crawled through the MS code and it seems like the compares (lessthan) are working correctly except in the case where the key cannot be found in the map.

I am using Visual Studio 2005 on Windows XP SP2, Dell 670 workstation. I'm at the end of my rope so I am hopeful that someone out there can see what I can't see.

thanks,
Cyndy
Jul 10 '07 #1
2 2124
weaknessforcats
9,208 Expert Mod 8TB
DWORD imageType = 0x1000007F;
SRCP_PROTS_LISTMAP m_ProtsListMap;
SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
SRCP_PROTS_LIST* pList = (*listMapIt).second; <<--

listMapIt.ptr->second = 0xbaadf00d
When the find fails, listMapIt is m_ProtsListMap.end(). That means listMapIt is garbage.

You need to:
Expand|Select|Wrap|Line Numbers
  1. DWORD imageType = 0x1000007F;
  2. SRCP_PROTS_LISTMAP m_ProtsListMap;
  3. SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
  4. SRCP_PROTS_LIST* pList;
  5.  
  6. if (listMapIt != m_ProtsListMap.end())
  7. {
  8.      plist = (*listMapIt).second;  
  9. }
  10. else
  11. {
  12.      //whatever you do when it's not found.
  13.  
Jul 11 '07 #2
When the find fails, listMapIt is m_ProtsListMap.end(). That means listMapIt is garbage.

You need to:
Expand|Select|Wrap|Line Numbers
  1. DWORD imageType = 0x1000007F;
  2. SRCP_PROTS_LISTMAP m_ProtsListMap;
  3. SRCP_PROTS_LISTMAP::iterator listMapIt = m_ProtsListMap.find(imageType);
  4. SRCP_PROTS_LIST* pList;
  5.  
  6. if (listMapIt != m_ProtsListMap.end())
  7. {
  8.      plist = (*listMapIt).second;  
  9. }
  10. else
  11. {
  12.      //whatever you do when it's not found.
  13.  
Thanks "WeaknessForCats" (I'm a cat lover so I appreciate your 'name'). What you suggested so makes sense and I will try and incorporate that into the logic somehow (this is not my code so I'm not exactly sure what the person needs to happen if it's not found). I am so curious why this used to work and doesn't now. No code changes were made in this file except to get rid of some compiler warnings for VS2005. I walked through the code using VS 6.0 and I get back the same result. However, the line of code
Expand|Select|Wrap|Line Numbers
  1. SRCP_PROTS_LIST* pList = (*listMapIt).second;
doesn't crash the application even when the iterator passed back is end(). Perhaps it's a case of several things being wrong that made it work right before.

anway, thanks again for taking the time to point out something that's pretty darn obvious now.
Jul 11 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: F. Petitjean | last post by:
I have written a script to find the modules which export the largest number of names. The gc.getreferrers(*objs) function gives also an idea of the dependencies between the modules. The code...
4
by: Matthias Hildebrand | last post by:
Hello, std::map< int, MyClass*> mymap; it = mymap.find( somekey ) // here happen bad things if( it != mymap.end() ) { // do something useful with it->second } else
3
by: Manuel Maria Diaz Gomez | last post by:
Hi everybody, This should be trivial, but I just can't see it. The following statement in a method doesn't work for me: (there's only one entry in myMap) map<const char*, int>::const_iterator...
3
by: rudymoore | last post by:
I'm often find-ing the same string in a map<string,xxx> I want to pass a hint to map::find but was disappointed to find that only map::insert takes the hint. Why would map::insert take a hint...
1
by: Jean Stax | last post by:
Hi ! I created a sample library project. In my second project I reference this library and make the following call, which returns "undefined value": Type myType =...
3
by: Ahmed Yasser | last post by:
Hi everybody, Can anyone tell how to call a standard "Find and Replace dialog box" while my application is at run time. Is there any embeded somewhere ?? or do i have to design one !! Thanks...
4
by: Ahmed Yasser | last post by:
Hi Everybody, can anyone help in sending code reference to build a simple find dialog box like the one we call by (ctrl+F). Thanks a lot.
6
by: Benny the Guard | last post by:
Been working on a project that compiles, links, and runs fine on 32-bit windows. Now trying to migrate to 64-bit windows. On debug build it works fine. However, on release build the std::map::find...
2
by: dhirajgaherwar | last post by:
Hi everybody, Can anyone tell how to call a standard "Find and Replace dialog box" while my application (embeded word aplication) is at run time. Is there any embeded somewhere ?? or do i have to...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.