Jump to content

  • Log In with Google      Sign In   
  • Create Account


Access Violation on Pointer Delete


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
6 replies to this topic

#1 Fl00Fy   Members   -  Reputation: 188

Like
0Likes
Like

Posted 16 March 2013 - 08:38 AM

Well I'm working on a small project, a Blackjack game in C++. I'm getting an Access Violation error when I try to delete a pointer to an object. 

Sprite* message;


void showMessage ()
{
	message->draw();
	glutSwapBuffers();
	Sleep(2000);
	delete message;
}

 

showMessage() is only called from the function below. 

void drawAll()
{
	showHands();
	showButtons();
	if (message != NULL)
	{
		showMessage();
	}
	glutSwapBuffers();
}

 

I have a feeling it's really stupid problem that I'll probably kick myself over when I found out what it is, but I can't, for the life of me, think of what it is. 

I think that's all the necessary code

Any help would be appreciated. 

 

It might be worth mentioning that drawAll() is called in the glutDisplayFunc... But I'm not sure if this would make any difference. 

Thanks



Sponsor:

#2 SiCrane   Moderators   -  Reputation: 9387

Like
0Likes
Like

Posted 16 March 2013 - 08:41 AM

How are you allocating message?

#3 Fl00Fy   Members   -  Reputation: 188

Like
0Likes
Like

Posted 16 March 2013 - 08:45 AM

Ah yes. Sorry. 

void initMessage (char* fileName)
{
	message = new AniSprite (100, 260, fileName, 1, 1);
}
void evaluateEndGameHandsWithMessage (int playerHandValue, int dealerHandValue)
{
	if (isPlayerWinner(playerHandValue, dealerHandValue))
	{
		if (isHandBlackJack(hand))
		{
			initMessage ("BlackJackMessage.bmp");
			drawAll();
		}
		else if (isHandFiveCardTricked(hand))
		{
			initMessage ("FiveCardMessage.bmp");
			drawAll();
		}
		initMessage ("WinMessage.bmp");
		drawAll();
	}
	else
	{
		initMessage ("LoseMessage.bmp");
		drawAll();
	}
}

 

Any other general advice on the code would also always be appreciated. 



#4 SiCrane   Moderators   -  Reputation: 9387

Like
2Likes
Like

Posted 16 March 2013 - 08:50 AM

Well, looking at it, at least one problem is that you don't set message to null after deleting it. The next time drawAll() is called without an intervening initMessage(), it tries to draw and delete an already deleted message.

#5 Fl00Fy   Members   -  Reputation: 188

Like
0Likes
Like

Posted 16 March 2013 - 09:01 AM

Ah I see. I should brush up on my pointers. I assumed deleting it would set it to NULL. 
Thanks so much for the help. It solved the problem.



#6 cardinal   Members   -  Reputation: 798

Like
2Likes
Like

Posted 16 March 2013 - 09:39 AM

You should also initialize the pointer to NULL. Pointers aren't NULL by default.

In general you need to initialize all your variables (they aren't defaulted on their own in release mode), otherwise they can create hard to track down bugs (especially in networked games).

#7 Fl00Fy   Members   -  Reputation: 188

Like
0Likes
Like

Posted 16 March 2013 - 09:51 AM

Alright. Thanks so much. 






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS