【问题标题】:Destructor delete wrong element析构函数删除错误的元素
【发布时间】:2018-10-29 19:34:39
【问题描述】:

我的纹理有一个小问题。

我想要那个:

  • m_texture 的纹理 ID 为 #5
  • 应该删除 ID 为 #5 的纹理。

问题:此时纹理 #5 被删除。

纹理在析构函数中自行删除。

inline void SetTexture(Texture texture) //texture -> #5, m_texture -> #7
{ 
    m_texture = texture;
    //m_texture -> #5
    //texture -> #5
    // But #5 is deleted, and I don't want that
}

[...]

private:
    Texture m_texture;

编辑:

这行得通:

Texture texture(0, 102, 153);
rect.SetTexture(texture);

但这不是:

rect.SetTexture(Texture(0, 102, 153));

编辑 2: 我认为这个问题将被关闭,因为它有很多代码。 [对不起]

纹理标题:

class Texture
{
public:
    Texture(const std::string& fileName);
    Texture(int red, int green, int blue);

    void Bind();
    virtual ~Texture();

protected:
private:

    void Init(unsigned char* imageData, int width, int height);
    GLuint m_texture;

};

纹理类:

Texture::Texture(const std::string& fileName)
{
    int width, height, numComponents;
    unsigned char* imageData = stbi_load((fileName).c_str(), &width, &height, &numComponents, 4);

    if (imageData == NULL)
        std::cerr << "Texture loading failed for texture: " << fileName << std::endl;

    Init(imageData, width, height);

    stbi_image_free(imageData);
}
Texture::Texture(int red, int green, int blue)
{
    unsigned char imageData[] = {
        static_cast<char>(red),
        static_cast<char>(green),
        static_cast<char>(blue)
    };

    Init(imageData, 1, 1);
}

void Texture::Init(unsigned char* imageData, int width, int height) {
    glGenTextures(1, &m_texture);
    glBindTexture(GL_TEXTURE_2D, m_texture);

    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); 
    glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, imageData);
}

Texture::~Texture()
{
    glDeleteTextures(1, &m_texture);
}

void Texture::Bind()
{
    glActiveTexture(GL_TEXTURE0);
    glBindTexture(GL_TEXTURE_2D, m_texture);
}

【问题讨论】:

  • 作为一个关于你的问题的可能提示:你关注the rules of three, five or zero吗?
  • @FreddyC。请自行研究。使用搜索引擎!
  • @FreddyC。我对shared_ptr 什么都没说,我说的是移动语义。 “它不起作用”只是意味着你做错了。你需要一个移动构造函数。至于投票:它们并不是为了帮助您,而是表明您的问题对其他人有多大用处。
  • 好吧,不知道你是如何定义Texture的,我们能提供什么帮助?如此写。你的问题没用,只值得投反对票……

标签: c++ destructor


【解决方案1】:

您的错误似乎在于您如何将 ID 分配给Textures。

由于您在销毁期间进行了删除,因此您的帖子中必须有一些您未共享的指针。

纹理对象可能没有跟随rule of three

如果你有一个副本、一个赋值或一个析构函数,那么你需要把这三个都做。


浏览您的代码当前正在做什么:

inline void SetTexture(Texture texture) //Texture::Texture(Texture) is called
{ 
    m_texture = texture; //Texture::operator= is called
} //Texture::~Texture() is called

理想的行为是:

  1. texture 中的 ID 应与作为参数传递的 ID 相同。
  2. 该 ID 应传递给 m_texture
  3. texture 被销毁时,它不是对 ID 的唯一引用,因此不会销毁它。

这是shared_ptr的原意。

【讨论】:

  • 谢谢,对我有点帮助。现在我用新的发现改变了我的问题。
  • 我们需要看看Texture类的内部。
  • 最好称之为“三/五/零规则”,重点是
  • 对不起,我是一个c++初学者,不懂这个规则。我实现了新的构造函数和运算符,但它仍然不起作用。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-11
  • 2014-05-27
  • 2016-07-08
  • 1970-01-01
  • 1970-01-01
  • 2017-04-06
相关资源
最近更新 更多