0xC0000005: Access violation reading location of an Integer?

12,987

Solution 1

According to the error message, your code is trying to access address 0x00000008, which is very close to 0. This means you probably have a null pointer of type Grid * somewhere and you are calling a function on it.

You should either ensure that the pointer is not null, or check it. For example:

Grid * grid = ...;
if (grid == NULL){ return; }
grid->move(0,1);

Note that NULL is the same as 0.

Solution 2

What is the value of 'this'? Are you trying to dereference a null pointer to an instance of Grid?

Share:
12,987
Fuller
Author by

Fuller

Game Engine Architect, Networking Programmer, and all-round lover of systems.

Updated on June 05, 2022

Comments

  • Fuller
    Fuller almost 2 years

    It seems normally people receive these errors when working with pointers, array, structs, etc. I'm getting it on every integer in a class, which confuses me. I'm sure I'm just missing some small technical detail (I'm a bit new to this).

    The exact error is:

    First-chance exception at 0x003e6616 in Mine.exe: 0xC0000005:
        Access violation reading location 0x00000008.
    Unhandled exception at 0x003e6616 in Mine.exe: 0xC0000005:
        Access violation reading location 0x00000008.
    

    The code breaks at the first line in this class method:

    void Grid::Move(int x, int y)
    {
    this->offX+=x;
    this->offY+=y;
    for (int i=0;i<2;i++)
        for (int k=0;k<2;k++)
            BitmapSetxy(chunks[i][k]->map,this->offX,this->offY);
    }  
    

    Here is the constructor of Grid:

    Grid::Grid()
    {
    totalW =320*2;
    totalH = 320*2;
    offX = 0;
    offY = 0;
    
    //Fill the chunks array with Maps to be used
    for (int i=0;i<2;i++)
        for (int k=0;k<2;k++)
            chunks[i][k] = new Map(i*320,k*320);
    
    //Everything starts as dirt
    for (int x=0;x<(320*2)/20;x++)
        for (int y=0;y<(320*2)/20;y++)
            blocks[x][y] = bType::Dirt; 
    }
    

    And the header file:

    #ifndef MapDef
    #define MapDef
    
    #include "Map.h"
    
    class Grid
    {
    private:
    int totalW, totalH;
    int offX, offY; 
    public:
    enum bType
    {
        Blank,
        Dirt,
        Copper
    };
    
    Map * chunks[2][2];
    bType blocks[32][48];   
    void RemoveBlock(int x, int y);
    bType GetBlockAt(int x, int y);
    int GetAbsolutePosition20(int);
    int GetMapIndex(int);
    int GetChunkRelative(int,int);
    bType GetBlockBelow(int x, int y);
    bType GetBlockAbove(int x, int y);
    bType GetBlockSide(int x, int y, bool isRight);
    void Move(int x, int y);
    Grid();
    };
    
    #endif
    

    When looking at the locals view of the current instance of Grid totalW, totalH, offX,offY all show the CXX0030 error, but the two arrays are perfectly fine. What exactly is going on here?

    EDIT:

    The definition of the grid pointer is stored in a separate little namespace that serves as something of a static class:

    namespace Engine
    {       
    static Grid * grid;
    static Player * player;
    }
    

    It is actually created here in the main cpp file:

    //Initialize engine
    Engine::grid = new Grid();
    Engine::player = new Player(160,240);
    

    Here is the excerpt where it is called, in another class called Player

        if (y>392 && y<480 && x>75 && x<152)
        {
            printf("Right");
            Engine::grid->Move(20,0);
        }
    

    Edit 2:

    I'm sorry, I forgot to remove the "static" keyword from the grid declaration in engine. I believe that was what was causing the problem.