0xC0000005:读取Integer的位置时发生访问冲突
0xC0000005: Access violation reading location of an Integer?
通常情况下,人们在处理指针、数组、结构等时都会收到这些错误。我在类中的每个整数上都得到了这些错误,这让我很困惑。我确信我只是缺少了一些小的技术细节(我对此有点陌生)。
准确的错误是:
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.
代码在这个类方法的第一行中断:
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);
}
以下是网格的构造函数:
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;
}
以及头文件:
#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
当查看Grid totalW的当前实例的locals视图时,totalH、offX、offY都显示CXX0030错误,但这两个数组非常好。这里到底发生了什么?
编辑:
网格指针的定义存储在一个单独的小名称空间中,该名称空间充当静态类:
namespace Engine
{
static Grid * grid;
static Player * player;
}
它实际上是在主cpp文件中创建的:
//Initialize engine
Engine::grid = new Grid();
Engine::player = new Player(160,240);
以下是在另一个名为Player 的类中调用它的摘录
if (y>392 && y<480 && x>75 && x<152)
{
printf("Right");
Engine::grid->Move(20,0);
}
编辑2:
很抱歉,我忘了从引擎中的网格声明中删除"static"关键字。我相信这就是造成问题的原因。
根据错误消息,您的代码正在尝试访问地址0x00000008,该地址非常接近0。这意味着你可能在某个地方有一个Grid *
类型的空指针,并且你正在调用它上的一个函数
您应该确保指针不是空的,或者检查它。例如:
Grid * grid = ...;
if (grid == NULL){ return; }
grid->move(0,1);
注意,NULL
与0相同。
"this"的值是多少?您是否试图取消引用指向Grid实例的空指针?
相关文章:
- 写入位置0x0000000C时发生访问冲突
- 0xC0000005:访问冲突写入位置0xCDCDCDCD动态分配错误
- 0xC0000005:访问冲突读取位置 0x00000000. 重载 == 运算符的问题
- (此函数用于计算 SMA).在 FA.exe 中0x00509159引发异常: 0xC0000005:访问冲突读取位置0
- 0xC0000005:读取位置0xCDCDCDC1时发生访问冲突
- 0xC0000005:读取位置0xCDCDCDCD时发生访问冲突
- 读取类的析构函数中的位置时发生访问冲突
- 在尝试使用CUDA分配内存时,我遇到了访问冲突写入位置错误
- 创建 2D 数组0xCCCCCCCC访问冲突写入位置
- 0x0F50DF58:0xC0000005:访问冲突读取位置0x0047CA04时未处理的异常
- 在 DLL 中嵌入 Python:Py_DECREF列表对象时访问冲突读取位置
- 在 C 代码中调用 Python 函数时第三次出现访问冲突写入位置错误
- 0xC0000005:访问冲突读取位置0x0000000000000008
- 使用动态数组访问冲突写入位置
- C++访问冲突读取位置0xDDDDDDCD当我尝试删除已更新的数组时
- 使用向量的函数崩溃,调试器说访问冲突读取位置
- vc++ 访问冲突读取位置
- C++ - 智能指针 - 访问冲突读取位置0xDDDDDDDD
- C++访问冲突读取位置错误
- 调用隔离后访问冲突执行位置0x0000000000000000:New()