Sign in to follow this  
Zmurf

Understanding Runtime Errors, Help?

Recommended Posts

Sometimes I get runtime errors in Visual Studio .Net but I don't understand what it's telling me. I recently received this one:
Quote:
Unhandled exception at 0x00415c25 in Peevish.exe: 0xC0000005: Access violation reading location 0x00000034.
I'd like to be able to fix the problem but I can't work out what the problem is because I can't understand what the error is telling me. Does anyone know how to work it out?

Share this post


Link to post
Share on other sites
By the looks of it you're trying to deference a NULL pointer.

If you're running in debug mode, it should show you the line that it's failing on. Try to stop it from dereferencing the pointer if it's NULL (Or work out why a NULL pointer was getting there).

For example:


cMyClass *bob = NULL;

// Both of these lines would cause an access violation
bob->m_member = 2;
bob->Function();


could be fixed by one of these:


cMyClass *bob = NULL;

// Check for NULL before attempting to access it
if(bob != NULL)
{
bob->m_member = 2;
bob->Function();
}


-----------------------------------------------------


cMyClass *bob = new cMyClass;

// 'bob' is no longer NULL
bob->m_member = 2;
bob->Function();

Share this post


Link to post
Share on other sites
that would make sence because im story some some classes on a vector.

Declaring it As:
vector<CPlayer*> m_Players;

And creating a new object in the vector like this:
m_Players.push_back(new CPlayer(0.0, 0.0, 1));

Then accessing it like this:
m_Players[0]->Render();


Where did I mess up? Also, how did you know that was the reson for the runtime error?

Share this post


Link to post
Share on other sites
it may be some internal pointer in the CPlayer class,

which is used during the "render" function...

access violations ( sometimes called segfaults ) are pretty common while you're learning to program. even after you can get them. they almost apply to pointers and accessing parts of memory your program should not access, like the memory allocated to another program.

bottom line, always initialise a pointer, to NULL if not anything else, and check for NULLness before dereferencing. when you produce the final product, you can eliminate such checking if you want

Share this post


Link to post
Share on other sites
Replace:
m_Players[0]->Render()
With:
m_Players.at(0)->Render()

at() works like [] except it will call an exception if you attempt to access a non-existant member of the vector,.

Share this post


Link to post
Share on other sites

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