gpt4 book ai didi

c++ - 引用计数器和析构函数被调用两次

转载 作者:行者123 更新时间:2023-11-28 02:23:20 25 4
gpt4 key购买 nike

我正在创建一个类 OpenGLManager,这个类的想法是管理对在 OpenGL 中分配的资源(如顶点数组对象和顶点缓冲区对象)的引用。

所以我想做的是,每当我分配一个新资源时,我都会将引用添加到此类中,并且每当使用该资源的对象被销毁(析构函数)时,对该资源的引用就会递减。我意识到的一个问题是,在某些情况下,一个对象的析构函数被调用两次,这使我的代码中断。 destructor called twice

我基本上是在创建类似 shared_ptr 的东西,但我有处理程序而不是指针。这是代码:

class OpenGLManager
{
public:
static OpenGLManager& getInstance()
{
static OpenGLManager instance;

return instance;
}
// Increment reference counter
void incrementVBO(GLuint vbo);
void incrementVAO(GLuint vao);
void incrementShaderProgram(GLuint program);
// Decrement reference counter
void decrementVAO(GLuint vao);
void decrementVBO(GLuint vbo);
void decrementShaderProgram(GLuint program);
private:
std::unordered_map<GLuint, int> _vbos;
std::unordered_map<GLuint, int> _vaos;
std::unordered_map<GLuint, int> _programs;

OpenGLManager() {};
OpenGLManager(const OpenGLManager&) = delete;
void operator=(OpenGLManager const&) = delete;

};
---------------------------------------------------------------
#include "opengl_manager.h"
using std::vector;

void lh::OpenGLManager::incrementShaderProgram(GLuint program)
{
if (_programs.find(program) == _programs.end())
_programs[program] = 1;
else
_programs[program]++;
}

void lh::OpenGLManager::incrementVAO(GLuint vao)
{
if (_vaos.find(vao) == _vaos.end())
_vaos[vao] = 1;
else
_vaos[vao]++;

}

void lh::OpenGLManager::incrementVBO(GLuint vbo)
{
if (_vbos.find(vbo) == _vbos.end())
_vbos[vbo] = 1;
else
_vbos[vbo]++;
}

void lh::OpenGLManager::decrementVAO(GLuint vao)
{
if (_vaos.count(vao) == 0)
{
std::cerr << "Trying to decrement VAO: " << vao << ". Fatal Error." << std::endl;
exit(EXIT_FAILURE);
}

_vaos[vao]--;

if (_vaos[vao] == 0) // There are no more references
{
auto iter = _vaos.find(vao);
if (iter == _vaos.end())
{
std::cerr << "Trying to remove inexistent hashmap (vao) key." << std::endl;
exit(EXIT_FAILURE);
}
_vaos.erase(iter);

glDeleteVertexArrays(1, &vao);
}
}

void lh::OpenGLManager::decrementVBO(GLuint vbo)
{
if (_vbos.count(vbo) == 0)
{
std::cerr << "Trying to decrement VBO: " << vbo << ". Fatal Error." << std::endl;
exit(EXIT_FAILURE);
}

_vbos[vbo]--;

if (_vbos[vbo] == 0) // There are no more references
{
auto iter = _vbos.find(vbo);
if (iter == _vbos.end())
{
std::cerr << "Trying to remove inexistent hashmap (vbo) key." << std::endl;
exit(EXIT_FAILURE);
}
_vbos.erase(iter);

glDeleteBuffers(1, &vbo);
}
}

void lh::OpenGLManager::decrementShaderProgram(GLuint program)
{
if (_programs.count(program) == 0)
{
std::cerr << "Trying to decrement Program: " << program << ". Fatal Error." << std::endl;
exit(EXIT_FAILURE);
}

_programs[program]--;

if (_programs[program] == 0) // There are no more references
{
auto iter = _programs.find(program);
if (iter == _programs.end())
{
std::cerr << "Trying to remove inexistent hashmap (program) key." << std::endl;
exit(EXIT_FAILURE);
}
_programs.erase(iter);

glDeleteProgram(program);
}
}

这似乎可行,除了当我使用 push_backstd::vector 上添加对象时,析构函数被调用两次,这会将引用减 2。我怎样才能避免这种情况?或者这只是一个糟糕的设计?

编辑

使用 OpenGLManager 的类的实现

lh::RawModel::RawModel(vector<GLfloat> vertices, vector<GLint> indices, GLenum usage) 
: _vao(-1), _indicesCount(indices.size())
{
std::cout << "CREATING MYSELF" << std::endl;
createAndBindVAO();
storeVerticesDataInVBO(0, vertices, 0, usage);
storeVerticesDataInVBO(1, vertices, VERTEX_SIZE * sizeof(GLfloat), usage);
storeIndicesDataInEBO(indices, usage);
unbindVAO();
}

lh::RawModel::~RawModel()
{
// Decrement reference counters for opengl resources
std::cout << "DESTROYING MYSELF" << std::endl;
lh::OpenGLManager::getInstance().decrementVAO(_vao);
lh::OpenGLManager::getInstance().decrementVBO(_vbo);
lh::OpenGLManager::getInstance().decrementVBO(_ebo);
}

const int lh::RawModel::VERTEX_SIZE = 3;

void lh::RawModel::storeIndicesDataInEBO(vector<GLint> indices, GLenum usage)
{
glGenBuffers(1, &_ebo);
lh::OpenGLManager::getInstance().incrementVBO(_ebo);

glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, _ebo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, indices.size() * sizeof(GLint), &indices[0], usage);
}

void lh::RawModel::storeVerticesDataInVBO(int iAttribute, vector<GLfloat> data, int offset, GLenum usage)
{
glGenBuffers(1, &_vbo);
lh::OpenGLManager::getInstance().incrementVBO(_vbo);

glBindBuffer(GL_ARRAY_BUFFER, _vbo);
glBufferData(GL_ARRAY_BUFFER, data.size() * sizeof(GLfloat), &data[0], usage);
glVertexAttribPointer(iAttribute, VERTEX_SIZE, GL_FLOAT, GL_FALSE, 2 * (VERTEX_SIZE * sizeof(float)), (GLvoid*)offset);
glEnableVertexAttribArray(iAttribute);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

void lh::RawModel::createAndBindVAO()
{
glGenVertexArrays(1, &_vao);
glBindVertexArray(_vao);
std::cout << "incrementing vao " << _vao << std::endl;
lh::OpenGLManager::getInstance().incrementVAO(_vao);
}

int lh::RawModel::getNumberOfIndices()
{
return _indicesCount;
}

GLuint lh::RawModel::getVAO()
{
return _vao;
}

void lh::RawModel::unbindVAO()
{
glBindVertexArray(0);
}

中断的调用示例:

models_.push_back(lh::RawModel(vertices, indices, GL_STATIC_DRAW));

最佳答案

这是预期的行为。

不幸的是,您的设计并不安全。如果你创建一个作用域变量,递增它,将它复制到作用域外的一个变量,那么作用域变量将被销毁(调用递减)然后,当你最终销毁另一个变量时,它会第二次递减,崩溃程序。像这样:

RawModel rawModel;
{
RawModel temp;
temp.increment() //calls increment methods.
rawModel = temp;
} // destructor called once to clean up temp;
// destroy rawModel. destructor called twice to clean up rawModel;

这正是发生的事情,将实例化 RawModel,然后将其传递给 vector,vector 将您的变量复制到内部,现在您有两个实例来调用析构函数并中断您的程序。

选项:1.(推荐)使用shared_ptr。2. 定义复制构造函数和运算符 =,将内容从一个 RawModel 复制到另一个 RawModel 并递增计数。

虽然如果你有空闲时间,第二个选项听起来更容易,但还是选择选项 1,学习 shared_ptr,它将对你以后的程序有很大帮助。

1.
shared_ptr<RawModel> rawModel = make_shared(new RawModel);
rawModel->increment() //increment stuff
vector.push_back(rawModel);
//now it'll work.

2.
//additional RawModel methods:
RawModel(const RawModel& rawModel) {
// copies everything and if it is initialized increment
}

const RawModel& operator =(const RawModel& other) {
//same as copy constructur
}

希望这对您有所帮助。

关于c++ - 引用计数器和析构函数被调用两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31527457/

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