尽管没有指针,但访问冲突读取位置 0x00000000
Access violation reading location 0x00000000 despite no pointers
除非我弄错了,否则 "Access violation reading location 0x00000000" 意味着您正试图引用一个尚未初始化的指针,这就是我被这个错误难倒的原因。下面粘贴的是我的代码,注释指示 visual studio 调试器告诉我错误发生的位置。这让我感到困惑,因为我传递给函数的参数中 none 是指针。有什么想法吗?
void Mesh::Render(Shader* shader)
{
glBindVertexArray(m_vao);
glEnableVertexAttribArray(shader->GetAttributeLocation("position"));
glVertexAttribPointer(0, 3, GL_FALSE, GL_FALSE, sizeof(Vertex), 0);
glDrawElements(GL_TRIANGLES, m_size, GL_UNSIGNED_INT, 0); // error here
glDisableVertexAttribArray(shader->GetAttributeLocation("position"));
glBindVertexArray(0);
}
m_size
声明为非指针整数
如果有帮助的话,调试器会将我带到一些不可用的源代码,因此调试器会指向反汇编中的这一行:
001DFEC7 mov edi,dword ptr [esi]
我不懂汇编,所以我不确定这是否有任何帮助。
编辑
如果有人想知道,我正在绑定使用 VAO 所需的元素数组缓冲区。剩下的Mesh
class在下面
Mesh::Mesh()
{
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ibo);
m_size = 0;
}
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
glDeleteBuffers(1, &m_ibo);
}
void Mesh::AddVertices(Vertex* vertices, int vertSize, int* indices, int indexSize)
{
m_size = indexSize;
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, vertSize * sizeof(Vertex), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * sizeof(int), indices, GL_STATIC_DRAW);
}
根据 this,最后一个参数是 "a pointer to the location where the indices are stored."
你有一个 0。所以它会尝试取消引用位置 0。
此处的其他两个答案仅部分正确。 glDrawElements
期望最后一个参数是指向某些索引的指针。但是,与其他答案的建议相反,如果此参数为 0 (NULL
) 也没关系,因为:
the indices
parameter of glDrawElements
[...] is interpreted as an offset within the buffer object measured in basic machine units.
如果您用 GL_ELEMENT_ARRAY_BUFFER
调用 glBindBuffer
。
如果这是您的尝试,则段错误意味着之前对 OpenGL 函数的调用失败并且您没有绑定有效的缓冲区。当您调用 glDrawElements
时,它没有可用的有效缓冲区,因此会出现段错误。 Check every OpenGL function call for success 找出失败的那个。
除非我弄错了,否则 "Access violation reading location 0x00000000" 意味着您正试图引用一个尚未初始化的指针,这就是我被这个错误难倒的原因。下面粘贴的是我的代码,注释指示 visual studio 调试器告诉我错误发生的位置。这让我感到困惑,因为我传递给函数的参数中 none 是指针。有什么想法吗?
void Mesh::Render(Shader* shader)
{
glBindVertexArray(m_vao);
glEnableVertexAttribArray(shader->GetAttributeLocation("position"));
glVertexAttribPointer(0, 3, GL_FALSE, GL_FALSE, sizeof(Vertex), 0);
glDrawElements(GL_TRIANGLES, m_size, GL_UNSIGNED_INT, 0); // error here
glDisableVertexAttribArray(shader->GetAttributeLocation("position"));
glBindVertexArray(0);
}
m_size
声明为非指针整数
如果有帮助的话,调试器会将我带到一些不可用的源代码,因此调试器会指向反汇编中的这一行:
001DFEC7 mov edi,dword ptr [esi]
我不懂汇编,所以我不确定这是否有任何帮助。
编辑
如果有人想知道,我正在绑定使用 VAO 所需的元素数组缓冲区。剩下的Mesh
class在下面
Mesh::Mesh()
{
glGenVertexArrays(1, &m_vao);
glGenBuffers(1, &m_vbo);
glGenBuffers(1, &m_ibo);
m_size = 0;
}
Mesh::~Mesh()
{
glDeleteVertexArrays(1, &m_vao);
glDeleteBuffers(1, &m_vbo);
glDeleteBuffers(1, &m_ibo);
}
void Mesh::AddVertices(Vertex* vertices, int vertSize, int* indices, int indexSize)
{
m_size = indexSize;
glBindVertexArray(m_vao);
glBindBuffer(GL_ARRAY_BUFFER, m_vbo);
glBufferData(GL_ARRAY_BUFFER, vertSize * sizeof(Vertex), vertices, GL_STATIC_DRAW);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, m_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indexSize * sizeof(int), indices, GL_STATIC_DRAW);
}
根据 this,最后一个参数是 "a pointer to the location where the indices are stored."
你有一个 0。所以它会尝试取消引用位置 0。
此处的其他两个答案仅部分正确。 glDrawElements
期望最后一个参数是指向某些索引的指针。但是,与其他答案的建议相反,如果此参数为 0 (NULL
) 也没关系,因为:
the
indices
parameter ofglDrawElements
[...] is interpreted as an offset within the buffer object measured in basic machine units.
如果您用 GL_ELEMENT_ARRAY_BUFFER
调用 glBindBuffer
。
如果这是您的尝试,则段错误意味着之前对 OpenGL 函数的调用失败并且您没有绑定有效的缓冲区。当您调用 glDrawElements
时,它没有可用的有效缓冲区,因此会出现段错误。 Check every OpenGL function call for success 找出失败的那个。