Archived

This topic is now archived and is closed to further replies.

JSCFaith

Pointer Problems: Linked List

Recommended Posts

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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites