Game Development Community

Error I can't explain..

by Tom Perry · in Torque Game Engine · 12/18/2007 (10:55 am) · 4 replies

Hello everyone, I'm having some trouble understanding what is going wrong with my code. I'm not an experianced C++ coder, but I can't see anything wrong with it. It occurs when the following function is called:

void connect(int direction) { _connections[direction] = 1;};

_connections is defined as:

int _connections[8];

This function is called like so:

fromNode->connect(4);

Where fromNode is a NavNode, the class wth the connect function above.
I don't understand why it is causing an error, I'd appriciate any help in on the matter!

#1
12/18/2007 (2:50 pm)
The most important info was left off your question: What was the error report? ;)
Most likely fromNode isn't correct. In an interactive debguger, you can look at the value of the fromNode pointer, and also all its members to verify that it is setup correctly at the time the line of code is executed.
#2
12/18/2007 (4:57 pm)
Doh, of course ;) Here is the error:

Unhandled exception at 0x0087f929 in ArcaneFX-Demo_DEBUG.exe: 0xC0000005: Access violation writing location 0xcfcfd00f.
It refers to the line with the function in. I can't find out if the node is a valid pointer, I just get a bunch of CXX0030 Errors in the run-time debugger.
This is the concsole function that calls the connect function:

ConsoleMethod( AIPathNodeGrid, connectNodes, void, 6, 6, "AIPathNodeGraph.connectNodes(X1,Y1,X2,Y2) - Connect 2 nodes, indicating you can access one from the other.")
{
	argc;
	if(dStrlen(argv[2]) == 0) {
		Con::errorf(ConsoleLogEntry::Script, "Zero length string sent with connectNode as X1");
		return;
	}
	if(dStrlen(argv[3]) == 0) {
		Con::errorf(ConsoleLogEntry::Script, "Zero length string sent with connectNode as X1");
		return;
	}
	if(dStrlen(argv[4]) == 0) {
		Con::errorf(ConsoleLogEntry::Script, "Zero length string sent with connectNode as X1");
		return;
	}
	if(dStrlen(argv[5]) == 0) {
		Con::errorf(ConsoleLogEntry::Script, "Zero length string sent with connectNode as X1");
		return;
	}

	S32 x1 = dAtoi(argv[2]);
	S32 y1 = dAtoi(argv[3]);
	S32 x2 = dAtoi(argv[4]);
	S32 y2 = dAtoi(argv[5]);
	
	AIPathNode* fromNode = object->getNode(x1,y1);
	AIPathNode* toNode = object->getNode(x2,y2);
	// these nodes will only connect NW,N,NE and E
	if (x1 > x2)
	{
		//we must be going NW
		fromNode->connect(5);
		toNode->connect(2);
	}
	if (x1 == x2)
	{
		//we must be gong N
		fromNode->connect(6);
		toNode->connect(1);
	}
	if (y1 == y2)
	{
		//we must be going east
		fromNode->connect(4);
		toNode->connect(3);
	}
	else
	{
		//we must be going NE
		fromNode->connect(7);
		toNode->connect(0);
	}
}
x1,x2,y1 and y2 are all set properly. I don't really follow whats going on! The getNode function just returns the node from the array:

AIPathNode* getNode(S32 x, S32 y) { return _nodes[x][y]; };
#3
12/18/2007 (5:17 pm)
0xC0000005 is pretty generic, but always seems to be a bad pointer.

In your line: AIPathNode* fromNode = object->getNode(x1,y1);
there is no check for an error. I don't know what this function uses for an error return, in the case that there is not a node with a value pair such as (x1, y1), but trying to use something like 0 (NULL) or -1 for a function pointer for your fromNode->connect(6) call will never work.

The first place I would look is to check for an error return after that line (as well as the one that follows), and if I get it, I would echo the values and return from the function without calling connect(). If that hunch played out, I would then try to figure out why my x1, y1 values are wrong.

<* Wes *>
#4
12/18/2007 (5:54 pm)
Well yep you're right, the toNode and fromNode aren't pointing to anything. But the x1,x2,y1 and y2 are set to the correct values... I added in these debug lines to the code:

AIPathNode* fromNode = object->getNode(x1,y1);
AIPathNode* toNode = object->getNode(x2,y2);
Con::printf(" x1 = %d", dAtoi(argv[2]));
Con::printf(" y1 = %d", dAtoi(argv[3]));
Con::printf(" x2 = %d", dAtoi(argv[4]));
Con::printf(" y2 = %d", dAtoi(argv[5]));
Con::printf(" fromNode = %d", fromNode);
Con::printf(" toNode = %d", toNode);

here is a snippit of the console log:

The 2 connecting node positions are 0 0 and 1 0
 x1 = 0
 y1 = 0
 x2 = 1
 y2 = 0
 fromNode = -808464433
 toNode = -808464433
The 2 connecting node positions are 0 0 and 0 1
 x1 = 0
 y1 = 0
 x2 = 0
 y2 = 1
 fromNode = -808464433
 toNode = -808464433
The 2 connecting node positions are 0 0 and 1 1
 x1 = 0
 y1 = 0
 x2 = 1
 y2 = 1
 fromNode = -808464433
 toNode = -808464433

...

The 2 connecting node positions are 1 2 and 2 2
 x1 = 1
 y1 = 2
 x2 = 2
 y2 = 2
 fromNode = -808464433
 toNode = -808464433
The 2 connecting node positions are 1 2 and 0 3
 x1 = 1
 y1 = 2
 x2 = 0
 y2 = 3
 fromNode = -808464433
 toNode = -808464433
The 2 connecting node positions are 1 2 and 1 3
 x1 = 1
 y1 = 2
 x2 = 1
 y2 = 3
 fromNode = -808464433
 toNode = -808464433
The 2 connecting node positions are 1 2 and 2 3
 x1 = 1
 y1 = 2
 x2 = 2
 y2 = 3
 fromNode = -808464433
 toNode = -808464433

...

etc etc for each node.
THankyou for your help so far. I'm new to C++, I have a feeling its to do with the pointers. Here is how _nodes is set up in the class:

private:
AIPathNode* _nodes[100][100];