• ### Popular Now

• 15
• 15
• 11
• 9
• 10

#### Archived

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

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

## Recommended Posts

Hey, I am working on a asteroids clone and I am having a big problem with linked lists. Right now, the way that my program stands, each object (ship, asteroids, bullets, particles, etc.) is in a linked list. I go through this list each frame and run each object's main function. That all works great, but whenever I need to delete a node from the list I have a big problem. Here general code I use and where it crashes.
  void RemoveExplosion(CExplosionNode *Node) { if(CExplosionNode::s_pFirst && CExplosionNode::s_pLast == Node) { CExplosionNode::s_pFirst = NULL; CExplosionNode::s_pLast = NULL; delete Node; <- Crashes here ///////////// } else { if(Node->m_pPrev == NULL) CExplosionNode::s_pFirst = Node->m_pNext; else Node->m_pPrev->m_pNext = Node->m_pNext; if(Node->m_pNext == NULL) CExplosionNode::s_pLast = Node->m_pPrev; else Node->m_pNext->m_pPrev = Node->m_pPrev; delete Node; <- Or crashes here ///////////// } } 
I have remote debugging, and I checked to see whether the pointers were good or not and they are. They are legit pointers, but for some reason, whenever I delete the node, the program crashes. It is really pissing my off, I have been trying to fix this for eons. Does anyone know why this is happening. Or, maybe even better. Is my approach to this object thing entirely wrong?? Are linked lists bad? Well if you could answer ether of these questions I would appreciate it. Thanks.. later Edited by - JSCFaith on October 22, 2001 12:03:10 AM Edited by - JSCFaith on October 23, 2001 11:01:39 AM

##### Share on other sites
Linked lists = good. Rolling your own linked lists = fair. Using STL''s std::list = very good.

What you might want to do is put traces in the ctor and the dtor of the value of "this" is, make sure you''re not deleting the same thing twice. Also trace the address of what you''re trying to delete. If you can''t use a TRACE macro or something, log it to a file.

##### Share on other sites
Try testing to make sure Node exists before you delete it. As it stands now, if both CExplosionNode::s_pLast and Node are NULL, then you will call the delete Node statement.

if (Node) delete Node; should help.

##### Share on other sites
Hey, Thanks for your help guys. I tried what you said and for some reason it still does not work. I logged the destructor information to a file. This is how the class hierarchy works.

COBject->CMultiFramed->CExplosion;

So when I log it too a file this is what happens. First, ~CExplosion() runs and is successful, then ~CMultiFramed() runs and it is successful aswell. Finnaly, ~CObject() runs and it is successful as well. So in escense, the object is total deleted. All the destructors are run, but for some reason it still crashes when I call, delete Node;

For some reason though, it only crashes if I am running through a loop like so.

  CExplosion *TempNode; // Temporary node used to carry contents of Node->m_pNext in the loop.for(CExplosionNode *Node = CExplosionNode::s_pFirst; Node != NULL; Node = Node->m_pNext) {if(Node->m_Data->GetState() == DEAD) {TempNode = Node->m_pNext;RemoveExplosion(Node); // This is the functions aboveNode = TempNode;}else {Node->m_Data->Main(); // Run the main function of the explosion}

Now this loop runs fine except when RemoveExplosion(). It crashes anytime I try to delete the Node. Like I said though, all the destructors are run and are successful, so I don't see why it is crashing.

If I run remote debugging and debug the program, it crashes at that exact line: delete Node;

Well if someone can help I would appreciate it. [/source]

Edited by - JSCFaith on October 23, 2001 11:03:54 PM

##### Share on other sites
Hi.

In the code above, inside the loop, you''ve declared the pointer
Node to point to an object of class CExplosionNode, but the
function RemoveExplosion treats it like a pointer to a CExplosion
object. Maybe you''ve got an error in the declaration of the
function or in the declaration of the pointer types.

I think you''re mixing pointers to CExplosion and
CExplosionNodes.

"If you''''re gonna die, die with your boots on"

##### Share on other sites
Oops that was a typo in the forum. It says CExplosoinNode in my code but it still crashes when I call "delete Node".

##### Share on other sites
Something doesn't seem right to me.

CExplosionNode::s_pLast and CExplosionNode::s_pFirst

Are these static objects?

Edited by - Invader X on October 23, 2001 8:33:22 PM

##### Share on other sites
Yes, they are static. They point to the beginning of the linked list and the end of the linked list.

##### Share on other sites
Well, maybe I should use that STL thing you were talking about Stoffell. Could anyone tell me what exactly that is? Also, where I can find more information about using it. Thanks.

Edited by - JSCFaith on October 23, 2001 11:17:50 PM

##### Share on other sites
I don''t know if this would help, but it works for me.
void updateList(void){ STRUCT *current; current=STRUCT1; while(current!=NULL) { if(current->state==INACTIVE) { STRUCT *temp; temp=current; if(temp==STRUCT1) { STRUCT1=temp->next; LocalFree(temp); temp=NULL; } else { STRUCT *prev; prev=STRUCT1; while(prev->next!=temp) prev=prev->next; prev->next=temp->next; if(temp!=NULL) { LocalFree(temp); temp=NULL; } } } current=current->next; }}
Using a simple single linked list. I couldn''t get it to work though unless all the artwork was from the same bitmap, and it goes through the whole list every time. Like I said, it works for me. Although when I get time I''d rather rework it as it strikes me as kinda inefficient.

"A man can''t just sit around." ''Lawn Chair'' Larry Walters (1982)