提问人:J. Doe 提问时间:4/8/2018 更新时间:4/8/2018 访问量:291
具有结构的类中的析构函数问题
Destructor issues in a class with structs
问:
我目前在为我的班级编写/使用析构函数时遇到问题。对于 OpenGL 赋值,我必须编写一个数据结构来保存有关模型的所有信息,请参阅下面的实现。这个类包含指向各种结构的指针,我已经学会了在堆上分配内存后正确清理。Model
Model.h
目前,我的应用程序在注释掉析构函数的同时工作正常,这确实给了我内存泄漏,我很确定我的讲师会因此给我一个显着降低的分数。
但是,在定义(取消注释)析构函数时,我遇到了问题。在运行一个名为的方法(请参阅下面的实现)后,我的析构函数被调用,该方法会引发应用程序中断异常:InitModels
我在这里错过了什么?我听说过并读过一些关于三法则的东西,这些东西可能与我的问题有关,但我被困在在我的案例中开始应用这条规则的地方。
这是我的方法:InitModels
void InitModels()
{
/*
Teapot model
*/
Model teapot("Teapot");
teapot.material = new Material(glm::vec3(0.0, 0.0, 0.0),
glm::vec3(0.0, 0.0, 0.0),
glm::vec3(1.0), 128);
teapot.mesh = new Mesh("Objects/teapot.obj");
teapot.modelMatrix = new ModelMatrix(glm::mat4());
teapot.texture = new Texture("Textures/Yellobrk.bmp", true, loadBMP("Textures/Yellobrk.bmp"));
teapot.transformations = new Transformations(true, 0.01f, glm::vec3(0.0f, 1.0f, 0.0f));
models.push_back(teapot);
}
这是我的:Model.h
#include <iostream>
#include <vector>
#include <GL/glew.h>
#include <GL/freeglut.h>
#include <glm/glm.hpp>
#include <glm/gtc/matrix_transform.hpp>
#include <glm/gtc/type_ptr.hpp>
#include "glsl.h"
#include "objloader.hpp"
#pragma once
struct Material
{
glm::vec3 ambientColor;
glm::vec3 diffuseColor;
glm::vec3 specular;
float power;
/*
Initializer list constructor
*/
Material(){ }
Material(glm::vec3 ambient, glm::vec3 diffuse, glm::vec3 spec, float pwr) :
ambientColor(ambient), diffuseColor(diffuse), specular(spec), power(pwr) { }
};
struct Mesh
{
char* fileLocation; // location of object file
vector<glm::vec3> vertices;
vector<glm::vec3> normals;
vector<glm::vec2> uvs;
/*
Initializer list constructor
*/
Mesh(char* fileLoc) : fileLocation(fileLoc) { }
Mesh(char* fileLoc, vector<glm::vec3> vert, vector<glm::vec3> normals, vector<glm::vec2> uvs) :
fileLocation(fileLoc), vertices(vert), normals(normals), uvs(uvs) { }
~Mesh() { }
};
struct ModelMatrix
{
glm::mat4 model;
glm::mat4 mv;
/*
Initializer list constructor
*/
ModelMatrix() { }
ModelMatrix(glm::mat4 model) : model(model) { }
ModelMatrix(glm::mat4 model, glm::mat4 mv) : model(model), mv(mv) { }
};
struct Texture
{
char* fileLocation; // location of texture file
bool applyTexture;
GLuint textureID;
/*
Initializer list constructor
*/
Texture() { }
/*Texture(char* fileLocation, bool applyTexture) :
fileLocation(fileLocation), applyTexture(applyTexture)
{
textureID = loadBMP(fileLocation);
}*/
Texture(char* fileLocation, bool applyTexture, GLuint textureID) :
fileLocation(fileLocation), applyTexture(applyTexture), textureID(textureID) { }
~Texture() { }
};
struct Transformations
{
bool rotationEnabled;
float angle;
glm::vec3 axis;
Transformations() { }
Transformations(bool rotEnabled, float angle, glm::vec3 axis)
: rotationEnabled(rotEnabled), angle(angle), axis(axis) { }
~Transformations() { }
};
class Model {
public:
Model(string modelName)
{
name = modelName;
}
~Model();
string name;
GLuint vao;
Material * material;
Texture* texture;
Mesh* mesh;
ModelMatrix* modelMatrix;
Transformations* transformations;
};
答:
“我听说过并读过一些关于三法则的东西,这可能与我的问题有关” 你在这一点上是正确的。https://en.wikipedia.org/wiki/Rule_of_three_(C%2B%2B_programming)
在堆栈上创建,然后将副本推送到向量中,
您没有定义复制构造函数,因此编译器会创建默认构造函数。它只是对指针进行简单复制。
一旦代码超出范围,就会调用此项的析构函数。并且指针变为无效,因此向量中的项现在具有无效的指针。
调用向量析构函数后,它会调用 和 You 在已删除指针上执行删除的析构函数。void InitModels()
Model
models.push_back(teapot);
Model
您可以使用 unique_ptr 并定义复制构造函数来解决此问题。
评论
class_name(class_name const&) = delete;
class_name& operator=(class_name const&) = delete;