if(temp == head)
{
head = head->Next;
delete temp;
}
Problems deleting nodes from linked list
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..
head->Next is NULL head is both head and tail...
this is driving me mad :(
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?
okay, this is where the delete is called
this is the message pumping loop where my violation comes
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; } }}
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
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
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]
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
Popular Topics
Advertisement