Access Violation Error Reading Location ...

I have a static list pointer for containing hgeRect objects, which is declared in a header. In this same header, I have a base class containing multiple functions (inlined), and also contains a single hgeRect object. In my derived classes I call an initialization function which pushes the inherited hgeRect object onto the rect stack. This is not called in the derived class's constructor.

Whenever the initialization function is called and the hgeRect object is being pushed onto the rect stack, I get the following error:

First-chance exception at 0x01181a73 in HGE Test.exe: 0xC0000005: Access violation reading location 0x00000014.


First-chance exception probably meaning that I can catch the exception and figure out what's going on, but that's just my guess. What exception class should I try to catch, if I need to catch one at all? I'm just considering trying to catch the exception to see what the problem is, if anyone knows what the problem might be beforehand, feel free to tell me or ask any questions to figure it out.

I'll upload the source if necessary.
Last edited on
"Access violation" is the key here. It means you're reading memory you shouldn't be. This is a sign of a bad pointer.

In this case, you're reading from address 0x00000014. Since that address is so close to zero, it's a good chance you're trying to read from a null pointer.

Since this is a linked ?list class? probably one of your node pointers is screwed up or something.

At any rate, this isn't a matter of catching exceptions, it's a matter of fixing your pointers so that you don't reference them unless they're good.

The nice thing about this error is that the program will crash on the line which actually has the problem, and you'll be able to see which pointer is bad pretty easily if you're using a debugger.
Last edited on
The hgeRects being pushed onto the stack are created statically, not dynamically:

Declaration:
1
2
// Base class member
hgeRect collisionRect;


Being pushed onto the stack:
1
2
// part of the initialization method
rectStack->push_back ( &collisionRect );


Declaration of List:
1
2
// In global namespace
static std::list<hgeRect*>* rectStack;


It's the code where the rect is being pushed onto the stack that's causing the error. So unless I'm deleting collisionRect and somehow setting it to location 0x00000014 without me knowing it, something's going wrong with the instantiation of collisionRect.

And before you wonder if I forgot to create a list for rectStack, I create it at the beginning of my WinMain function.


EDIT: I just had an idea that maybe it's an issue with the order of the instantiation of my classes, since I have a few objects declared globally. I think that it shouldn't be an issue since I use pointers for objects, and just leave handles and built-in types normal, but I'll look into that.
Last edited on
The hgeRects being pushed onto the stack are objects, not pointers:


They're not, really. You're pushing pointers (notice that the list is of hgeRect* (pointer) and not hgeRect (object).

It looks like you're pushing pointers to ?temporary? objects, which means the pointers go bad as soon as the object goes out of scope. Unless the object never goes out of scope. It's hard to tell from these code snippits. Either way, it doesn't look like you're doing this right.

Anyway... I'm positive this is a bad pointer problem... and I'm 99.9% sure you're trying to dereference a null pointer. When the program crashes, take a look in the debugger at the line of code causing the crash, and look at the value of all variables involved in the line of code. I'm sure one of them will be a null pointer.


EDIT: yeah global initialization order might be a problem too. There are ways around that though
Last edited on
I know I got those terms wrong, I fixed them in an edit.

Alright, I changed the rectStack from being a pointer to a list to just being a regular list. At some point I must've been calling the constructor of an object before the stack was made. But now I have another problem, which is that the objects aren't being pushed onto the stack. I'm not sure why this is, but the size of the rectStack at run-time is 0. Have you ever heard of something like this happening?
Last edited on
Topic archived. No new replies allowed.