gpt4 book ai didi

c++ - 在C++中删除数组导致程序崩溃

转载 作者:行者123 更新时间:2023-11-30 02:29:34 25 4
gpt4 key购买 nike

我正在关注有关 opengl(图形编程库)的 youtube 教程。我创建了名为顶点、ShapeData 和 ShapeGenerator 的类。总体思路是我正在创建代码,该代码将保存我决定想出并想显示在屏幕上的任何形状类型的数据。问题是一旦在 cleanup() 函数的 ShapeData.h 中命中第一个“delete[]”,我的程序似乎就会崩溃。相关代码如下:

顶点.h

#pragma once
#include "GLM/glm.hpp"

class Vertex
{
public:
Vertex();
Vertex(glm::vec3 thePosition, glm::vec3 theColor);
glm::vec3 position;
glm::vec3 color;
};

顶点.cpp

#include "Vertex.h"

Vertex::Vertex()
{}

Vertex::Vertex(glm::vec3 thePosition, glm::vec3 theColor) :
position(thePosition),
color(theColor)
{
}

形状数据.h

#pragma once
#include "Vertex.h"
#include "GL/glew.h"

struct ShapeData
{
ShapeData() :
verticies(0), numberOfVerts(0),
indicies(0), numberOfIndicies(0)
{}
Vertex* verticies;
GLuint numberOfVerts;
GLushort* indicies;
GLuint numberOfIndicies;
GLsizeiptr VertexBufferSize() const
{
return numberOfVerts * sizeof(Vertex);
}
GLsizeiptr IndexBufferSize() const
{
return numberOfIndicies * sizeof(GLushort);
}
void CleanUp()
{
delete[] verticies;
delete[] indicies;
verticies = 0;
indicies = 0;
numberOfIndicies = 0;
numberOfVerts = 0;
}
};

形状生成器.cpp

#include "ShapeGenerator.h"


ShapeData ShapeGenerator::MakeTriangle()
{
Vertex triangle[] = {
Vertex(glm::vec3(0.0f, 1.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f)),
Vertex(glm::vec3(-1.0f, -1.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f)),
Vertex(glm::vec3(1.0f, -1.0f, 0.0f), glm::vec3(1.0f, 0.0f, 0.0f))
};
ShapeData shapeData;

shapeData.numberOfVerts = sizeof(triangle) / sizeof(*triangle);
shapeData.verticies = new Vertex[shapeData.numberOfVerts];
memcpy(shapeData.verticies, triangle, sizeof(triangle));
shapeData.verticies = triangle;

GLushort indicies[] = { 0,1,2 };
shapeData.numberOfIndicies = sizeof(indicies) / sizeof(*indicies);
shapeData.indicies = new GLushort[shapeData.numberOfIndicies];
memcpy(shapeData.indicies, indicies, sizeof(indicies));

return shapeData;
}

我正在尝试创建一个三角形,并且在不运行 main 中的 cleanup() 函数的情况下一切正常。这是我在 main 中调用 Cleanup() 的部分:

主要.cpp

ShapeData triangle = ShapeGenerator::MakeTriangle();

GLuint bufferID;
glGenBuffers(1, &bufferID);
glBindBuffer(GL_ARRAY_BUFFER, bufferID);
glBufferData(GL_ARRAY_BUFFER, triangle.VertexBufferSize(), triangle.verticies, GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, 0);
glEnableVertexAttribArray(1);
glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 6, (char*)(sizeof(float) * 3));

GLuint indexBufferID;
glGenBuffers(1, &indexBufferID);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, indexBufferID);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, triangle.IndexBufferSize(),triangle.indicies, GL_STATIC_DRAW);

triangle.CleanUp();

最佳答案

你在这里替换了 new[]' 指针。这会导致崩溃,因为三角形不是新的[]。

shapeData.verticies = new Vertex[shapeData.numberOfVerts];
memcpy(shapeData.verticies, triangle, sizeof(triangle));
shapeData.verticies = triangle;

关于c++ - 在C++中删除数组导致程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39333636/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com