Sign in to follow this  

Strange Segmentation Fault, Stack corruption?

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

Well I was just plugging along on my game and adding an object creation function witch creates a random object at a random possition and a random velocity. I didn't think it would cause a whole lot of problems because there are few pointers used and most of it is just work with ints and floats. I finished the function and it seems to work right but I'll randomly get an error report when the function is called (I say randomly because it doesn't happen all the time). When I ran the debugger I am pointed to the line indecated in the code. It sounds like a stray pointer but the only pointers used are to instances of singletons and in std::lists. heres the code:
void Object_Creator::Create_Object()
{
    assert(Object_List.size() > 0);
    int Index = rand() % Object_List.size();
    int Velocity = (rand() % ((Game_Speed + Object_List[Index]->Base_Velocity) * 2)) -  (Game_Speed + Object_List[Index]->Base_Velocity);
    int X_Vel = rand() % Velocity;  //SEGMENTATION FAULT
    int Y_Vel = rand() % Velocity;
    float X, Y;
    if(rand() % 2)
    {
	if(rand() % 2)
	{
	    X = 1;
            Y = (rand() % 100) / 100.0f;
         }
	else
	{
	    X = -1;
            Y = -((rand() % 100) / 100.0f);
         }
    }
    else
    {
	if(rand() % 2)
	{
	    Y = 1;
            X = (rand() % 100) / 100.0f;
        }
	else
        {
	    Y = -1;
            X = -((rand() % 100) / 100.0f);
        }
    }
    if(X_Vel == 0)
        X_Vel = 1;
    if(Y_Vel == 0)
	    Y_Vel = 1;
    new Game_Objects(X,Y,0,.1,.1,X_Vel,Y_Vel,Object_List[Index]->Image);
}





thanks for your time Overview: object_list is a vector of "flat" objects used souly in the production of real objects (Game_Objects). The lack of assignment of the new Game_Objects is intentional because the class auto-adds itself to a linked list

Share this post


Link to post
Share on other sites
Likely your memory is corrupted by overwriting array bounds somewhere else in the program.

Share this post


Link to post
Share on other sites
As it turns out there wasnt any stack corruption. I ran the debugger a few times and I noticed that every time I got an error the value of velocity was 0 at first I thought it was a coincidence but then it hit me. rand() % involves division so when velocity was 0 there was a divide by 0 error. problem solved [grin]

Share this post


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