Advertisement

*groan* Code help needed

Started by March 08, 2001 08:56 PM
14 comments, last by Radhil 23 years, 11 months ago
Can someone tell me what''s wrong with this...
  
class QUEUE
{
public:
	QUEUE();
	~QUEUE();
	void AddEvent(EVENT*);		// Adds event to the queue

	EVENT* PopEvent(void);		// Removes and returns next item in queue

private:
	EVENT* Next;	//Pointer to first item in queue

};

EVENT* QUEUE::PopEvent()
{
	Event("Queue: Event popping * ");

	EVENT* CurrEvent;		// keep pointer to first item in queue


	Event("Queue: Pointer ready * ");
// PROGRAM STOPS COLD AND DUMPS BACK TO WINDOWS AFTER THIS POINT

	if(Next == 0) Event("Next is NULL * ");
	if(Next != 0) Event("Next is not NULL * ");

	Event("Queue: Survived Test * ");

	CurrEvent = Next;

	Event("Queue: Event Acquired * ");

	Next = CurrEvent->NextEvent;		// advance the queue, which drops the first item


	Event("Queue: Queue Advanced * ");

	return CurrEvent;					// return the dropped first item

}
  
All the Event crap is just spitting text to a file so I know exactly where the program stopped. Problem is, the program seems to stop as soon as Next is referenced. Originally, the CurrEvent = Next; line was dropping the program, but now also the if statements fail. And I haven''t the slightest clue why. Both QUEUE constructor and AddEvent work properly. Only PopEvent does this. Help? Please? Radhil Trebors Persona Under Construction
Radhil TreborsPersona Under Construction
try
if (Next == NULL)
doWhatever;
if (Next != NULL)
doWhatever;


"Now go away or I shall taunt you a second time"
- Monty Python and the Holy Grail
Advertisement
Nope, that's not it. Tried it, just to be sure, but program still stops at the same point. The IF statements weren't the original crash point anyway, I just added them to see what Next's value would be, and it turns out they crash too.

Besides, what would whitespace have to do with a crash?

Radhil Trebors
Persona Under Construction

Edited by - Radhil on March 8, 2001 10:18:42 PM
Radhil TreborsPersona Under Construction
Hmm... I notice that you use the pointer even after you discover it''s null. Should be:

  CurrEvent = Next;  if (CurrEvent == 0)  {    Event("Next is NULL * ");  }  else if (CurrEvent != 0)  {    ...  }  return CurrEvent; 


Right now, it''s dying cuz you call CurrEvent->NextEvent when CurrEvent is NULL. Hope that helps

Jinushaun
CurrEvent = Next;
Next = CurrEvent->NextEvent;
If Next was NULL CurrEvent->NextEvent will bomb
No, that''s not it. I don''t mean to be rude to those who want to help, but read the post. It stops exactly where I''ve inserted the comment that says PROGRAM STOPS COLD AND DUMPS TO WINDOWS AFTER THIS POINT. All those Event lines output those text quotes to a file, and the very last one I have is "Pointer Ready". NOTHING after that point is executed because nothing comes after that in my log file. The program just dumps. And I just don''t get it.

Radhil Trebors
Persona Under Construction
Radhil TreborsPersona Under Construction
Advertisement
Oh yeah, don''t forget to clean up the memory!!!! This is VERY important.

I usually declare two pointers in these cases: one to delete with and one to store the value to return.

Jinushaun
Hmm... that means the if statement is killing it. Very odd. I think you should show your other functions so we can get a full understanding of how everything works.

I''m currently programming something that dies continuously in the destructor, so I understand how you''re feeling. Dynamic memory is a bitch.

Jinushaun
Here's the rest:
    QUEUE::QUEUE(){	Next = NULL;	EVENT* InitEvent = new EVENT;	InitEvent->EventType = BET_RECALCTURNS;	InitEvent->EventRound = 0;	InitEvent->EventChar = 0;	InitEvent->NextEvent = NULL;	AddEvent(InitEvent);}QUEUE::~QUEUE(){	EVENT* CurrEvent;	while(Next)	// Cycle through entire queue and delete	{		CurrEvent = Next;		Next = CurrEvent->NextEvent;		delete CurrEvent;	}}void QUEUE::AddEvent(EVENT* NewEvent){	EVENT* CurrEvent = Next;	EVENT* LastEvent = NULL;	while(CurrEvent)	{		if(CurrEvent->EventRound > NewEvent->EventRound)			break;		LastEvent = CurrEvent;		CurrEvent = LastEvent->NextEvent;	}	if(LastEvent)	// If LastEvent exists, link it to new	{		LastEvent->NextEvent = NewEvent;	}	else			{		Next = NewEvent;	}	NewEvent->NextEvent = CurrEvent;		if(Next == NULL) Event("WARNING: Next is NULL * ");// Please note, this warning does not appear in my log file	Event("Queue: AddEvent: Exiting... * ");}    


Queue is a member of a larger structure that is also allocated. Should this make a difference?


Radhil Trebors
Persona Under Construction

Edited by - Radhil on March 8, 2001 11:04:33 PM
Radhil TreborsPersona Under Construction
Just to be curious but what does Event() do?

Don''t use 0, use NULL.

You aren''t assigning NULL to all your deallocated memory pointers. Essentially the program thinks that your memory is still there, and still try''s to access it.

This topic is closed to new replies.

Advertisement