Jump to content
  • Advertisement
Sign in to follow this  
Sync Views

debugging errors

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

I keep getting a "vector subscript out of range" error after a while. The problem is I have no idea where as the assertion error just gives a line number from the vector header file :( I think it might have something to do with my object list iterators but they shouldn't ever go outside the vectors range and if an object is deleted the pointer for it is set to NULL so that the iterator doesn't try to do anything with that value.It doesn't seem to happen at any percifc point just within around 30 seconds in which time plenty of objects have been created and destroyed (the other vectors are static at this point). This is the only vector that could change during an iteration. But it's only the value it self that changes (pointer being set to NULL) so the range should stay the same as when the iterator started...
//object constructure
class object
{
public:
	object()
	{
		x = 0;
		y = 0;
		active = true;
		collision_id = -1;

		cgm::object_list.push_back(this);
		id = cgm::object_list.size()-1;
	}
//deconstruture
	~object(){cgm::object_list[id] = NULL;}
...
};

//iterator example
std::vector<object*>::iterator it;
for (it = object_list.begin(); it != object_list.end(); it++)
	if((*it)!=NULL && (*it)->active) (*it)->step1();

Share this post


Link to post
Share on other sites
Advertisement
What call stack? I looked at http://www.gamedev.net/reference/programming/features/debugging/page3.asp but the buttons it's decribing there I can't find in vs2008 :(

EDIT: Ok well I got the program to output some text to a file before each section. Found what section was before the crash etc and repeated till I found the problem


step_start();
obj->step1
Object 1 of 23
obj_ball
Object 2 of 23
obj_ball
Skipping object 3 of 23
Object 4 of 23
obj_ball
Object 5 of 23
obj_ball
Object 6 of 23
obj_ball
Object 7 of 23
obj_ball
Object 8 of 23
obj_ball
Object 9 of 23
obj_ball
Object 10 of 23
obj_ball
Skipping object 11 of 23
Skipping object 12 of 23
Skipping object 13 of 23
Skipping object 14 of 23
Skipping object 15 of 23
Skipping object 16 of 23
Skipping object 17 of 23
Skipping object 18 of 23
Skipping object 19 of 23
Skipping object 20 of 23
Skipping object 21 of 23
Object 22 of 23
proj_bullet:
Moved
Checking for collision
END

Object 23 of 23
proj_bullet:
Moved
Checking for collision
END

obj->step2
obj->step3
draw_start();
obj->draw
draw_end();
step_end();
LOOP END

step_start();
obj->step1
Object 1 of 23
obj_ball
Object 2 of 23
obj_ball
Skipping object 3 of 23
Object 4 of 23
obj_ball
Object 5 of 23
obj_ball
Object 6 of 23
obj_ball
Object 7 of 23
obj_ball
Object 8 of 23
obj_ball
Object 9 of 23
obj_ball
Object 10 of 23
obj_ball
Skipping object 11 of 23
Skipping object 12 of 23
Skipping object 13 of 23
Skipping object 14 of 23
Skipping object 15 of 23
Skipping object 16 of 23
Skipping object 17 of 23
Skipping object 18 of 23
Skipping object 19 of 23
Skipping object 20 of 23
Skipping object 21 of 23
Object 22 of 23
proj_bullet:
Moved
Offscreen Deleteing...
Checking for collision

The crash happens when a bullet goes offscreen and is deleted but then goes on to test of collision causeing a crash because it no longer exists...

offending code block

virtual void step1()
{
file << "\t\tproj_bullet:\n";
x += (float)lengthdir_x(speed, direction);
y += (float)lengthdir_y(speed, direction);
file << "\t\tMoved\n";
if (x < 0 || x > window_width || y < 0 || y > window_height)
{
file << "\t\tOffscreen Deleteing...\n";
delete this;//shouldn't this function exit here?
}
file << "\t\tChecking for collision\n";
obj_ball* collision = collision_check(this, obj_ball);
if (collision)
{
file << "\t\tCollided with ball\n";
collision->health -= 1;
delete this;
}
file << "\t\tEND\n\n";



Share this post


Link to post
Share on other sites
delete this;//shouldn't this function exit here?

^^ that line. Why don't you just return; after it so it exits? Otherwise it seems your "collision_check" function is attempting to use a null pointer at some point or other, as others have said, run it through the debugger and use the callstack. If you can't find the callstack window, when VC++ is debugging, click Debug > Windows > Callstack.

Share this post


Link to post
Share on other sites
Quote:
Original post by Sync Views
What call stack? I looked at http://www.gamedev.net/reference/programming/features/debugging/page3.asp but the buttons it's decribing there I can't find in vs2008 :(


MSVC has something called a "menu bar". It appears below the title bar at the very top of the window. There are words there. The last word there is "Help". Click on this word, and it will bring up a new list of other words. This list of words is called a "menu". This menu presents you with a number of different ways of accessing information on how to use MSVC. For example, if you select the "Index" (again, by clicking on it) it will open up a new window. This window contains the documentation for MSVC. Now go to the edit box labelled "Look for:" and type "call stack" and it will bring you to a number of different topics, one of which is labelled "viewing source code for functions on the call stack". Clicking on that topic will bring up an article named "How to: Use the Call Stack Window".
Quote:

To display the Call Stack window in break mode or in run mode
  • On the Debug menu, choose Windows and then click Call Stack.


So when MSVC is in run mode or break mode, (for example, when the application has stopped due to an assertion error like you have been dealing with) click on the word "Debug" on the menu bar. This will bring up another menu. The top word there will be "Windows" and there will be a little triangle to the right of it. Click on that word. It will open a new menu to the right of the current menu. One of the lines will be "Call Stack". Click on that line.

Share this post


Link to post
Share on other sites
This "objects register themselves in a container and commit suicide" business is serious bad mojo. The objects become dependant on the container for their existence, which inhibits reusability.

Instead, use external code to create objects and add them to the vector, and to run passes over the vector ('step1' etc. - BTW, for the love of all that is holy, please do come up with a more descriptive name). Have objects mark themselves as "dead" at the appropriate times, and then just sweep out the dead objects after all the passes using the erase-remove idiom.

Share this post


Link to post
Share on other sites
Turned out it was the creation of objects that sometimes invalidated an iterator (I'm presuming because the vector reallocated it's self to make room).
Ive made it so new objects are not added to the object_list untill a safe point.


if (!object_new.empty())
{
for (it = object_new.begin(); it != object_new.end(); it++)
{
object_list.push_back((*it));
(*it)->id = object_list.size()-1;
}
object_new.clear();
}



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!