0
\$\begingroup\$

I am making this class QuadTemplate

class QuadTemplate
   {
    public:
    QuadTemplate()
    {
        vbo=0;
        uvbuffer=0;
        vao=0;
        TextureID=0;
        vboType=0;
        drawStart=0;
        drawCount=0;
        programID=0;
        TextureShaderID=0;
    }

    ~QuadTemplate()
    {
        glDeleteBuffers(1, &vbo);
        glDeleteBuffers(1, &uvbuffer);
        glDeleteProgram(programID);
        glDeleteTextures(1, &TextureShaderID);
        glDeleteVertexArrays(1, &vao);
    }


    GLuint vbo;
    GLfloat vboData[12];
    GLenum vboType;

    GLuint uvbuffer;
    GLfloat uvDate[8];
    GLenum uvType;

    GLuint vao;

    GLint drawStart;
    GLint drawCount;

    GLuint TextureID;
    GLuint TextureShaderID;
    GLuint programID;
};

this class like the name says is a template where I store all the Vbo's date, it doesn't have any other function because I have a function that give it the values, the problem is when I try to draw the Vbo, it doesn't work, not only that it also presents some graphics mistakes, but when I comment the destructor, it work perfectly fine. why it doesn't work with the destructor?

\$\endgroup\$
  • 1
    \$\begingroup\$ Because you're destroying it while you're still using it? Sounds like you need to find out when the destructor is actually getting called. \$\endgroup\$ – MichaelHouse Jul 31 '13 at 14:56
3
\$\begingroup\$

Obviously the destructor get´s called at some point and deletes your VBO before you draw it, my guess is that you create a temporary object somewhere, which get´s deleted or have an issue with copies or assignments.

Example:

QuadTemplate quad;

void foo()
{
    QuadTemplate quad2();
    quad2.vbo = CreateVBO(); //function that creates VBO
    quad = quad2;
    //... do stuff
}//destructor for quad2 is called here

This first creates an object "quad2", then create quad2´s VBO. Afterwards you call the assign operator for your class, which copies all the data from "quad2" to "quad". When you then reach the end of the function "foo" the object "quad2" gets out of scope and the destructor is called, which will delete your vbo. when you use quad later on its vbo is deleted since both instances referred to the same vbo.

You can find those situations by either putting a breakpoint into your destructor or defining a private assignment-operator and copy-constructor to get compiler errors when you try to access those in code.

It´s rather difficult to solve these situations, you should either try to avoid all calls of the destructor unless you really want to or try to change your design to avoid that problem entirely. And allways create copy-constructors and assignemnt operators, although this is likely to cause performance trouble since you have to interact with the GPU alot to create a second VBO whenever you copy your data to another object.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ "FACEPALM" thanks man you say exactly what I was doing but finally I solved Passing my object to the function as a pointer so it doesn't erase my data at the end. \$\endgroup\$ – user33283 Jul 31 '13 at 15:37
  • \$\begingroup\$ Youre welcome, i changed the example, the old one wouldn´t cause the error since you didn´t create any VBO in the constructor. Hope this doesn´t cause any confusion. \$\endgroup\$ – Luis W Jul 31 '13 at 15:48
  • \$\begingroup\$ no don't worry, in fact this class is part of a bigger one called QuadInstance, where I have my model matrix,an object QuadTemplate is store as a pointer, so when I creates the base object QuadTemplate, I pass this as a pointer to all the instances of QuadInstance, This way I have one vbo with a different model matrix in each object, but i don't know if this was the best way to do it. I know that opengl has its own way to make instance but I don't Know how to use it, what do you thing about my method is it ok? \$\endgroup\$ – user33283 Jul 31 '13 at 16:15
  • \$\begingroup\$ This is not really the place for discussing that i think since it´s not directly related to your original question, but the basic idea is fine, i usually go for a similar approach, but how to do something liek this and especially the details of the implementation are diffrent from case to case. \$\endgroup\$ – Luis W Jul 31 '13 at 16:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.