Sign in to follow this  

Recommended Posts

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

Share this post


Link to post
Share on other sites
NotAYakk    876
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.

Share this post


Link to post
Share on other sites
BloodLust666    104
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

Share this post


Link to post
Share on other sites
NotAYakk    876
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.

Share this post


Link to post
Share on other sites
BloodLust666    104
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?

Share this post


Link to post
Share on other sites
NotAYakk    876
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?

Share this post


Link to post
Share on other sites
BloodLust666    104
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?

Share this post


Link to post
Share on other sites
daerid    354
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?

Share this post


Link to post
Share on other sites
BloodLust666    104
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

Share this post


Link to post
Share on other sites
Zahlman    1682
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.

Share this post


Link to post
Share on other sites
BloodLust666    104
yep, you're right. I have an object in main(), that object has a template I have created which HAS a map<> template. then in a function i pass that first object and access the map object and use the add() function to add another item. can i not use a pointer with an array like that?

Share this post


Link to post
Share on other sites
daerid    354
Quote:
Original post by EvilKnuckles666
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


So I repeat, why are you getting that error? If you're strictly using good ol' C++, with .NET nowhere to be found, you should never see the text "System.NullReferenceException" (unless of course you put it in your project yourself).

If you are, then there's something else going on. A null pointer in C++ gives you an access violation, not a NullReferenceException.

Check your project settings to make sure you're not building a .NET application.

Share this post


Link to post
Share on other sites
BloodLust666    104
well, the error isn't really in MY code, it's in the map<> template code, it the head heap or something like that, not really sure how the code behind that works, i just know how to use it.

but no, i KNOW i'm not using .NET; if it's in the project settings i wouldn't know there, where would that be?

Share this post


Link to post
Share on other sites
Zahlman    1682
Quote:
Original post by EvilKnuckles666
yep, you're right. I have an object in main(), that object has a template I have created which HAS a map<> template. then in a function i pass that first object and access the map object and use the add() function to add another item. can i not use a pointer with an array like that?


Show the relevant code in main() and the other function, please.

Share this post


Link to post
Share on other sites
BloodLust666    104
there's a whole lot to my code... i have a pretty full game engine.. but here's the code that shows it.


bool cGun::Fire(Wrath2D::cWUtility::sWVector3D Pos)
{
if (Wrath2D::cWUtility::Math.GetTicks() - m_lCurTimeFired >= m_lDelay)
{
Wrath2D::cWTextureSprite *pTexSpr = NULL;
Wrath2D::cWCollisionObject *pCollObj = NULL;

pTexSpr = m_pGraphics->EntityManager.AddUniqueSprite("Bullet");
pTexSpr->Init(m_pBulletType->pTexture, m_pBulletType->Rect, Pos);
pTexSpr->SetDirection(Wrath2D::cWUtility::Math.GetVectorAngle(
Wrath2D::cWUtility::sWVector2D(0.0f, 0.0f), m_pBulletType->Velocity));
pTexSpr->SetVelocity(m_pBulletType->Velocity.Length());

pCollObj = m_pEffects->CollisionSystem.AddUniqueObject("Bullet");
pCollObj->Init(pTexSpr, BULLET);
pCollObj->SetCollisionCircle(Wrath2D::cWUtility::sWVector2D(0.0f, 0.0f), float(pTexSpr->GetFrameHeight())/2.0f);

return true;
}
return false;
}

Type* AddUnique(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();
//std::map <std::string, Type*>::iterator Iter;
long i=1;
std::stringstream ss;


ss << RefName << i;

Iter = m_MapList.find(ss.str());

while (Iter != m_MapList.end())
{
i++;
ss.str("");
ss << RefName << i;

Iter = m_MapList.find(ss.str());
}

cWUtility::ErrorLog.Write(ss.str());

m_MapList.insert( std::pair<std::string, Type*>(ss.str(), newType));

return newType;
};

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this