Program crashes with STD::map find()

Started by
28 comments, last by nullsquared 16 years, 8 months ago
Hello, I have a function that crashes my program when it tries to find an object in a map. Here is the code. Is it possible to see why this might be happening because I am stuck on this one...
void CollisionSet::addCollision( GameComponent* first, GameComponent* second )
{
    // adds the collision to the first component's list
    std::map< GameComponent*, CollisionQueue* >::iterator firstComponent = this->_collisionMap->find( first );
    
/*if ( firstComponent == this->_collisionMap->end() )
    {
        CollisionQueue* firstQueue;
        this->_collisionMap->insert( std::make_pair( first, firstQueue ) );
    }
Everything in the function is commented out, so this has to be the problem. Thanks
Advertisement
Check your pointer to make sure it is pointing to a valid object. Thats my first guess. (The _collisionMap pointer)
Well I'll add something to check it directly, but they should be valid since I do some things with them before calling that function. I just realized that it's almost impossible for anyone to determine the problem from what I posted, so I'll look over it some more. Thanks anyways.
I'm talking about something like the following...You never actually create a map object to point to, so it crashes.

#include <map>int main(){	std::map<int *, char *> * pMap;	std::map< int*, char* >::iterator first = pMap->find( NULL );	return 0;}


needs to be:

#include <map>int main(){	std::map<int *, char *> * pMap = new std::map<int*,char*>;	std::map< int*, char* >::iterator first = pMap->find( NULL );	delete pMap;	return 0;}


It might also be that your object has been destructed and your this pointer is garbage.
Are you sure you are wanting to insert a non-assigned pointer into your _collisionMap.

From your code:

CollisionQueue* firstQueue; // this is not pointing to a valid object. are you handling this?
this->_collisionMap->insert( std::make_pair( first, firstQueue ) );
Ah right, I did forget to add that. However, it still crashes. What a pain in the ass! :P
Add the rest of your code so we can look at more of it... Please point out which line it is crashing on too. Thanks.
Quote:Original post by RuntCreature
Check your pointer to make sure it is pointing to a valid object. Thats my first guess. (The _collisionMap pointer)


If "find()" isn't crashing, and either "end()" or "insert()" is where the crash happens, it's likely that the pointer points to "something", but that the vtable is messed up.

It would help to see how the map instance is created... as you suggest.
my_life:          nop          jmp my_life
[ Keep track of your TDD cycle using "The Death Star" ] [ Verge Video Editor Support Forums ] [ Principles of Verg-o-nomics ] [ "t00t-orials" ]
Ok here is the constructor:

CollisionSet::CollisionSet(){    // initialize the collision set    this->_collisionMap = new std::map< GameComponent*, CollisionQueue* >();}


and here is the whole function. I commented out everything then started with the first line which happened to crash the game. So that's how I figured that it is the problem.

void CollisionSet::addCollision( GameComponent* first, GameComponent* second ){    // adds the collision to the first component's list    std::map< GameComponent*, CollisionQueue* >::iterator firstComponent = this->_collisionMap->find( first );    if ( firstComponent == this->_collisionMap->end() )    {        CollisionQueue* firstQueue;        this->_collisionMap->insert( std::make_pair( first, firstQueue ) );    }        Collision firstCollision( second );    firstComponent->second->addCollision( &firstCollision );    // adds the collision to the second component's list    std::map< GameComponent*, CollisionQueue* >::iterator secondComponent = this->_collisionMap->find( second );    if ( secondComponent == this->_collisionMap->end() )    {        CollisionQueue* secondQueue;        this->_collisionMap->insert( std::make_pair( second, secondQueue ) );    }        Collision secondCollision( first );    secondComponent->second->addCollision( &secondCollision ); }


I'll have to fix that make_pair too. A buddy of mine wrote this code, so I'm just trying to fix his bugs.
I noticed that

firstComponent->second->addCollision( firstCollision );

Won't work if it is pointing to _collisionMap->end(), so I made sure that firstComponent points to the new element when the new pair is made. Also, after fixing that, it will do the initial search for first without crashing. I'm going to test it to see if it's working properly now.

This topic is closed to new replies.

Advertisement