Skip to main content
gamedev.net
🔒 Locked

Pointer Problems: Linked List

Started by JSCFaith Aug 10, 2001 at 6:15 PM 3 replies 250+ views
Original Post
JSCFaith
JSCFaith
Hey, whenever I run this function below I get this error, at the line specified: "Unhandled exception in Program.exe: at 0xC0000005: Access Violation:" Well, I know becasue of the 0xC0000005 that the problem is with a pointer not pointing at anything, but I do not see what is wrong with my code.
  
// Delete TileTypes

void RemoveNode(TileType *Node) {

	// Release DirectDraw Surface

	Node->lpddTileSurface->Release();
	Node->lpddTileSurface = NULL;

	// Remove from list

	if(Node->pPrev == NULL) 
		TileType::pFirst = Node->pNext;
	else 
		Node->pPrev->pNext = Node->pNext; // This line screws up.


	if(Node->pNext == NULL) 
		TileType::pLast = Node->pPrev;
	else 
		Node->pNext->pPrev = Node->pPrev;

	delete Node;
	Node = NULL;

}
  
Well if you could help I would appreciate it. THanks
Buster
Buster
put this in the first line of the function:

if (!Node) return;
jaxson
jaxson
I don''t see anything wrong at first glance. You should probably check to make sure that Node != NULL before you do anything in your function, but obviously that isn''t your problem if your program is surviving the Release()

Are you sure that your pPrev pointer is always initialized? I would look in your constructor or the code that you use to add a new node to the list to see if you are either forgetting to set the pPrev member altogether or there is some case where it does not get set. That''s all the help I can give, I''m afraid. It looks like the code you posted should work.
Graham
Graham
In that first else you should try this:
else
{
if(Node->pNext == NULL)
{
Node->pPrev->pNext = NULL;
}
else
{
Node->pPrev->pNext = Node->pNext;
}
}


Go on an Intense Rampage
Go on an Intense Rampage
Kylotan
Kylotan
It looks like pPrev is an invalid pointer that isn''t NULL. So somewhere else in your program you''re probably setting it incorrectly.

PS: These 2 lines...

Node->lpddTileSurface = NULL;
Node = NULL;

...are unnecessary and should probably be removed. This is a religious matter, but essentially they don''t do anything useful except confuse a reading programmer as to the intent of the code. You don''t need to set lpddTileSurface to NULL as no code should be reading that particular pointer any more (it gets deleted by the end of the function). Similarly, setting Node to NULL at the end of the function is pointless as it goes out of scope with at very next line.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.