Understanding Runtime Errors, Help?

Started by
5 comments, last by Zmurf 18 years, 6 months ago
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?
If it's possible for us to conceive it, than it must be possible.
Advertisement
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 violationbob->m_member = 2;bob->Function();


could be fixed by one of these:

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


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

cMyClass *bob = new cMyClass;// 'bob' is no longer NULLbob->m_member = 2;bob->Function();
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?
If it's possible for us to conceive it, than it must be possible.
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
i didn't use any pointers in my render function tho
If it's possible for us to conceive it, than it must be possible.
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,.
ah kool thx
If it's possible for us to conceive it, than it must be possible.

This topic is closed to new replies.

Advertisement