Search code examples
c++pointersopenglgraphicsdelete-operator

delete array in C++ causing program crash


I am following along with a youtube tutorial concerning opengl (a graphics programming library). I've created classes called vertex, ShapeData and ShapeGenerator. The overall idea is I'm creating code which will hold data for any type of shape I decide to come up with and want to display to the screen. The problem is my program seems to crash once the first "delete[]" is hit within ShapeData.h in the cleanup() function. Here is the relevant code:

Vertex.h

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

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

Vertex.cpp

#include "Vertex.h"

Vertex::Vertex()
{}

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

ShapeData.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;
    }
};

ShapeGenerator.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;
}

I'm trying to create a triangle and everything works fine without running the cleanup() function within main. Here is the portion where I'm calling Cleanup() in main:

main.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();

Solution

  • You are replacing the new[]'d pointer here. This causes a crash because triangle is not new[]'d.

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