Sign in to follow this  

Any clue why this would cause an Unhandled Exception

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

Ok in my program I have typedef'd something like this. typedef unsigned short int BOUNDSACTION; const BOUNDSACTION BA_STOP = 0, BA_WRAP = 1, BA_BOUNCE = 2, BA_DIE = 3; and in a class I have have a BOUNDSACTION variable called m_baBoundsAction Ive written in the Accessor Methods a function to return the current value for m_baBoundsAction. But when I ever this Accessor Function is called it generates an Unhandled Exception. This be how I wrote it. BOUNDSACTION GetBoundsAction() {return m_baBoundsAction;}; Doesn't seam like anything special to me. As void SetBoundsAction(BOUNDSACTION ba) {m_baBoundsAction = ba;}; seams to work fine.

Share this post


Link to post
Share on other sites
It doesn't look like a problem with those functions, so I can't see a problem from that. I'd suspect the problem arises with the variable you are assigning the result to in the calling function. If you post the code for the calling function and maybe the full class definition, should be able to spot what's wrong.

Share this post


Link to post
Share on other sites
Well I changed everything to an Enum like you suggested. Im basing my code off a older book. ^-^; Well I get the Unhandled Exception when ever I add the following commented line.


//BOUNDSACTION BATemp = _pGolfBallSprite[_iDragBall]->GetBoundsAction(); <- this seams to do it
if(_pGolfBallSprite[_iDragBall] != NULL && _bDragging == TRUE)
{
_pGolfBallSprite[_iDragBall]->SetBoundsAction(BA_STOP);

// Move the sprite to the mouse cursor position
_pGolfBallSprite[_iDragBall]->SetPosition(x - (_pGolfBallBitmap->GetWidth() / 2),
y - (_pGolfBallBitmap->GetHeight() / 2));
}
_pGolfBallSprite[_iDragBall]->SetBoundsAction(BATemp);
}



and the exact unhandled exception is...
First-chance exception at 0x00411fa6 in JPong.exe: 0xC0000005: Access violation reading location 0x0000002c.
Unhandled exception at 0x00411fa6 in JPong.exe: 0xC0000005: Access violation reading location 0x0000002c.

Hope that helps.

Share this post


Link to post
Share on other sites
_pGolfBallSprite[_iDragBall] is NULL.

you are either not allocating the memory, overstepping the bounds of the array, or are deleting the pointer somewhere else and not removing it from your array.

according to the debugger, what is the value of the pointer held at that point in the array?

-me

Share this post


Link to post
Share on other sites
Yeah I figured that out just befffore you posted. So I added a few if statements to check if that pointer is ever NULL like so.


BOUNDSACTION BATemp = BA_STOP;
if(_pGolfBallSprite[_iDragBall] != NULL)
BATemp = _pGolfBallSprite[_iDragBall]->GetBoundsAction();

if(_pGolfBallSprite[_iDragBall] != NULL && _bDragging == TRUE)
{

_pGolfBallSprite[_iDragBall]->SetBoundsAction(BA_STOP);

// Move the sprite to the mouse cursor position
_pGolfBallSprite[_iDragBall]->SetPosition(x - (_pGolfBallBitmap->GetWidth() / 2),
y - (_pGolfBallBitmap->GetHeight() / 2));
}
if(_pGolfBallSprite[_iDragBall] != NULL)
_pGolfBallSprite[_iDragBall]->SetBoundsAction(BATemp);



Just wish I didn't have to have the SAME if statement so many times

Share this post


Link to post
Share on other sites
Quote:
Original post by Dko
Just wish I didn't have to have the SAME if statement so many times


You don't.

BOUNDSACTION BATemp = BA_STOP;
if(_pGolfBallSprite[_iDragBall] != NULL) {
BATemp = _pGolfBallSprite[_iDragBall]->GetBoundsAction();

if(_bDragging == TRUE) {
_pGolfBallSprite[_iDragBall]->SetBoundsAction(BA_STOP);

// Move the sprite to the mouse cursor position
_pGolfBallSprite[_iDragBall]->SetPosition(
x - (_pGolfBallBitmap->GetWidth() / 2),
y - (_pGolfBallBitmap->GetHeight() / 2));
}

if(_pGolfBallSprite[_iDragBall] != NULL)
_pGolfBallSprite[_iDragBall]->SetBoundsAction(BATemp);
}

Share this post


Link to post
Share on other sites
Quote:
Original post by drakostar
Quote:
Original post by Dko
Just wish I didn't have to have the SAME if statement so many times


You don't.

*** Source Snippet Removed ***

I would prefer:

assert(_pGolfBallSprite[_iDragBall] != 0)
//no more ifs

...or...

if(_pGolfBallSprite[_iDragBall] == 0) throw Something;
//no more ifs

Chances are, this is a symptom of a problem elsewhere [likely _iDragBall not being set properly]...silently ignoring it is not the correct solution.

There are other, higher level, alternatives, of course. For instance, if he replaced his raw array with a vector [either of objects or of smart pointers to objects], he could get automatic bounds checking via .at(), which would throw an exception on its own.

CM

Share this post


Link to post
Share on other sites

This topic is 4112 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this