Segfault when accessing a vector

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

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 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 on other sites
Hmm, nope. Still segfaults, same place. Thanks for the suggestion though :), I'll keep that in mind.

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 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).

• 10
• 17
• 9
• 13
• 41