Jump to content
  • Advertisement
Sign in to follow this  
Tokiko

Program crashes with STD::map find()

This topic is 3997 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
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 ) );

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!