Jump to content
  • Advertisement

Archived

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

JSCFaith

Linked list Pains....

This topic is 6107 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

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 this post


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


Link to post
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.

Invader X
Invader''s Realm

Share this post


Link to post
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 above

Node = 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 this post


Link to post
Share on other sites
Guest Anonymous Poster
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 this post


Link to post
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".

Sorry about that.

Share this post


Link to post
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 this post


Link to post
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)

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!