Sign in to follow this  

Segfault when accessing a vector

This topic is 4686 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

Hey all, I'm getting a segfault when I try to access a vector of game objects, which I've checked and looks completely correct. Segfault only happens when the vector is empty (i.e. last shot is destroyed). Here's some code that demonstrates this:
	if ( g_Shots.size() != 0 )
	{
		std::vector< SkyObject * >::iterator index = g_Shots.begin();
		while ( index != g_Shots.end() )
		{
			SkyObject *obj = *index;
			if ( obj )
			{
                                // This is one location of a segfault
				if ( obj->m_Param[1] == SHOT_PLAYER )
				{
					obj->m_PosY -= 4;
					if ( obj->m_PosY < -32 )
					{
						if ( g_Shots.size() == 1 )
						{
							g_Shots.clear();
						}
						else
						{
                                                        // This appears to be another
							g_Shots.erase( index );
						}
						delete obj;
						std::cout << "Shot destroyed.\n";
					}
				}
			}
			index++;
		}
	}



Note that even with the added g_Shots.size() == 0 checks, it still segfaults. The shots are being created with:
	if ( CheckBtn( SDL_BUTTON_LEFT ) )
	{
		SkyObject *shot = new SkyObject();
		shot->m_PosX = g_MouseX;
		shot->m_PosY = g_MouseY;
		shot->m_Param[0] = WPN_MACHINEGUN;
		shot->m_Param[1] = SHOT_PLAYER;
		g_Shots.push_back( shot );
		std::cout << "Shot fired.\n";
	}



GDB tells me that there may have been a double free or corruption, if that helps. Can anyone help me figure out what's wrong? Everything looks right AFAIK. Thanks in advance! [Edited by - fyhuang on February 12, 2005 4:43:42 PM]

Share this post


Link to post
Share on other sites
clear and erase invalidate iterators. erase returns a new valid iterator, clear does not.


while ( index != g_Shots.end() )
{
SkyObject *obj = *index;
if ( obj )
{
// This is one location of a segfault
if ( obj->m_Param[1] == SHOT_PLAYER )
{
obj->m_PosY -= 4;
if ( obj->m_PosY < -32 )
{
if ( g_Shots.size() == 1 )
{
g_Shots.clear();
break;
}
else
{
// This appears to be another
index = g_Shots.erase( index );
}
delete obj;
std::cout << "Shot destroyed.\n";
}
}
}
index++;
}

Share this post


Link to post
Share on other sites
Are you sure it segfaults with/by the code you posted? It looks fine to me (with my changes). Well i added a memory leak, but that shouldn't segfault.

Are there any other places where pointers are added to this vector?

BTW: If the body of the loop is executed on an empty vector you somewhere have broken it. .begin()!=.end() is false on an empty vector

Share this post


Link to post
Share on other sites
GDB says that the code segfaults at those two places, so I guess I'll have to trust that...

The vector is not accessed in any way besides the code I just listed (and a drawing routine, but that doesn't modify it), and also at the beginning (a simple clear() ) and at the end (commenting out the one at the end didn't stop the segfault, so I'll assume that that part has no relevance).

Share this post


Link to post
Share on other sites

This topic is 4686 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.

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