Jump to content
  • Advertisement
Sign in to follow this  
Muppi

Problems deleting nodes from linked list

This topic is 5411 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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 :(

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!