Problems deleting nodes from linked list

Started by
5 comments, last by Klotus 19 years, 6 months ago
Hello, i got a double linked list and when i call delete on a node i get runtime failiure, my debugger says Acces violation in this part, it works perfectly if i run the same piece without the deleting..

if(temp == head)
{
        head = head->Next;
	delete temp;
}

head->Next is NULL head is both head and tail... this is driving me mad :(
Things i felt yesterday, don''t matter any more.
Advertisement
You'll need to show some more code. Odds are you're trying to access the memory pointed to by temp somewhere after it's deleted. In particular, could you show us the entire function that code is from?
"Voilà! In view, a humble vaudevillian veteran, cast vicariously as both victim and villain by the vicissitudes of Fate. This visage, no mere veneer of vanity, is a vestige of the vox populi, now vacant, vanished. However, this valorous visitation of a bygone vexation stands vivified, and has vowed to vanquish these venal and virulent vermin vanguarding vice and vouchsafing the violently vicious and voracious violation of volition. The only verdict is vengeance; a vendetta held as a votive, not in vain, for the value and veracity of such shall one day vindicate the vigilant and the virtuous. Verily, this vichyssoise of verbiage veers most verbose, so let me simply add that it's my very good honor to meet you and you may call me V.".....V
okay, this is where the delete is called
int O3VServerNetwork::Get_Msg(int clientNr){	char buffer[1];	Clients *temp = head;	while(temp != NULL && temp->ID != clientNr)		temp = temp->Next;	if(temp == NULL)		return 1;	int nBytes = recv(temp->clientSocket, buffer, 1, 0);		if(nBytes == SOCKET_ERROR)	{		O3VLog::write(WARNING, "Could not recive message!");		return -1;	}	switch(buffer[0])	{		default:			cout <<"Client "<<temp->ID <<" has disconnected: " << temp->clientIp <<" ("<<--Clients_Connected<<" still online)" <<endl;			//DisClient(temp->ID);			closesocket(temp->clientSocket);			if(tail == head)			{				delete head;				head = NULL;				tail = NULL;			}			else if(temp == head)			{				head = head->Next;				delete temp;			}			else if(temp == tail)			{				tail->Prev->Next = NULL;				tail = tail->Prev;				delete temp;			}			else			{				temp->Prev->Next = temp->Prev;				temp->Next->Prev = temp->Prev;				delete temp;			}			return 1;			break;		case '2':			cout <<"Client :" << temp->clientIp << " did a send!" << endl;			return 1;			break;		case '3':			cout <<"Client :" << temp->clientIp << ", says Hi!" << endl;			return 1;			break;	}	return 1;}


this is the message pumping loop where my violation comes

oid O3VServerNetwork::Check_Message(){	fd_set input_set, exc_set;	int s, maxnfds;	timeout.tv_sec = 0;	timeout.tv_usec = 0;	FD_ZERO(&input_set);	FD_ZERO(&exc_set);	maxnfds = 0;	Clients *loopTemp1 = head;	while(loopTemp1 != NULL)	{		FD_SET(loopTemp1->clientSocket, &input_set);		FD_SET(loopTemp1->clientSocket, &exc_set);		if(maxnfds < loopTemp1->clientSocket)		{			maxnfds = loopTemp1->clientSocket;		}		loopTemp1 = loopTemp1->Next;	}	s = select(maxnfds+1, &input_set, NULL, &exc_set,&timeout);	if(s > 0)	{		Clients *temp = head;		while(temp != NULL)		{ 			if(FD_ISSET(temp->clientSocket, &exc_set))			{				//DisClient(temp->ID);			}			if(FD_ISSET(temp->clientSocket, &input_set))			{				Get_Msg(temp->ID);			}			temp = temp->Next;		} 	}}
Things i felt yesterday, don''t matter any more.
Perhaps you should consider using the standard library list type or a least separate the operations of your linked list into the actual linked list type instead of having it all mixed in other modules/types.
The commented DisClient(temp->ID)
is for deleting nodes but when i started getting errors i wanted to know why so i tried doing this to make sure right node gets deleted or something like that :P
Things i felt yesterday, don''t matter any more.
clicky [smile]
For starters I think you need to change some of your code.

OLD CODE
else
{
temp->Prev->Next = temp->Prev;
temp->Next->Prev = temp->Prev;
delete temp;
}

NEW CODE
else
{
temp->Prev->Next = temp->Next;
temp->Next->Prev = temp->Prev;
delete temp;
}

If you have 3 links
#1 Next->2 Prev->NULL
#2 Next->3 Prev->1
#3 Next->NULL Prev->2

If you deleted Node #2 for instance your links would be screwed up.
#1 Next->1 Prev->NULL
DELETED #2 Next->3 Prev->1
#3 Next->NULL Prev->1

Once this happens the above while loop becomes an infinite loop
while(temp != NULL && temp->ID != clientNr)
temp = temp->Next;

Take all of this with a grain of salt because I could of missed it entirely. Just my .02

[Edited by - Klotus on September 29, 2004 6:01:32 AM]

This topic is closed to new replies.

Advertisement