map

Started by
13 comments, last by BloodLust666 17 years, 8 months ago
is there a reason that map::find() would fail?? it's giving me an error saying: "An unhandled exception of type 'System.NullReferenceException' occurred in Phyer Shooter.exe Additional information: Object reference not set to an instance of an object." my map is defined as map<std::string, myType> and all i'm doing is testing Iter = map::find("somestring") and it's failing and giving me that error
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Advertisement
Quote:Iter = map::find("somestring")


Do you mean = or ==?

...

std::map<std::string, myType>.find(std::string) can "fail" in the following ways:

1> It can return map.end() if it can't find the std::string as a key in the map.

2> If operator lessthan crashes/throws an exception, it can throw an exception.

3> If you change the ordering of elements in a map without taking them out of the map and putting them back in, undefined behaviour can result.

4> If your operator lessthan isn't "consistant", undefined behaviour can result. (ie, if a<b but b<a, or a<b and b<c, but a is not less than c, etc).

5> If you have memory corruption that screws up the std::map's memory, undefined behaviour can result.
Type* Add(std::string RefName)
{
Type *newType = new Type();
std::map <std::string, Type*>::iterator Iter;
std::stringstream ss;

ss << RefName;

Iter = m_MapList.find(ss.str()); // fails here
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
Type* Add(std::string RefName)
{
Type *newType = new Type();
std::map <std::string, Type*>::iterator Iter;
std::stringstream ss;

ss << RefName;

Iter = m_MapList.find(ss.str()); // fails here


Daymn. :) Ok, step 1, simplify, and test multiple simple cases.

Change "Add" to:
Type* Add(std::string RefName){  typedef std::map <std::string, Type*> mymap;  // the next set of code is an attempt to see what crashes.  // if the next set of code doesn't crash, then it has no side effects:  mymap map_test; // a stand alone temp map  map_test.find(RefName); // a failed search  mymap::iterator Iter1 = map_test.find(RefName); // a failed search with assign  map_test[RefName] = 0; // put something in the temp map  map_test.find(RefName); // a successful search  Iter1 = map_test.find(RefName); //  a successful search, with assign  m_MapList.find(RefName); // the search in the real map, but with no assignment  // Ok, and now we try what the original code did:  mymap::iterator	Iter = m_MapList.find(RefName); // does this crash?  Type *newType = new Type();  // .. the rest of the function follows}


Does it crash at the same spot, or earlier? Does it not crash anymore?

I removed the stringstream object because, honestly, it wasn't needed in the code you provided.
i use the stringstream later on in the function for something else

but the code you had failed when it got to:
m_MapList.find(RefName); // the search in the real map, but with no assignment

what's that mean?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
i use the stringstream later on in the function for something else

but the code you had failed when it got to:
m_MapList.find(RefName); // the search in the real map, but with no assignment

what's that mean?


Your std::map "m_MapList" has been corrupted somehow.

A few ways it could have been corrupted:
1> Did someone already delete the "this" object you are working on?
2> Is your "this" pointer null?
3> Random memory corruption.
4> Someone changed one of the keys in the std::map on it.
5> Something else!

Possibility 3 4 and 5 are hard to track down. So following the path of least resistance, let's examine your "this" pointer.

What is the value of "this"? (either look at it in the debugger, or use printf("0x%08x\n", this);)

Is it null? (if so, we have a solution!)

What debugger are you using? Does it mark deleted memory -- and is the memory pointed to by the "this" pointer deleted?

Can you log all deletes of objects of the type causing the problem? Do any of them match the "this" pointer that is being called with the crash?

uhoh... how'd that happened? when i viewed the "this" pointer, i looked at the value of the map<> template and it said it has an <undefined value>... how would that happen?
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
The more important question is this:

Why are you getting a System.NullReferenceException, which is a .NET Framework class, in your C++ project?

Are you using C++/CLI?
daerid@gmail.com
yep, that's the error i'm getting, it's coming up in a new window and giving me that error i posted in i think my second post here. but i'm using strictly C++ not managed or anything
-------------------------Unless specified otherwise, my questions pertain:Windows Platform (with the mindset to keep things multi-platform as possible)C++Visual Studio 2008OpenGL with SFML
Quote:Original post by EvilKnuckles666
uhoh... how'd that happened? when i viewed the "this" pointer, i looked at the value of the map<> template and it said it has an <undefined value>... how would that happen?


The instance of the object upon which you are calling the Add() member function is somehow not valid (bad pointer, scribbled upon by some out-of-bounds array access, etc.). Undefined value suggests that the map value hasn't been initialized; but there's no way that could happen assuming you got passed the object constructor; this in turn suggests that you're calling Add() upon a pointer-to-object where the pointer doesn't actually point at a valid object.

This topic is closed to new replies.

Advertisement