0

I recently started to learn OpenGL (ver. 3.3 to be specific) and I also wanted to use some object oriented design for some features. For example, I want to create a class called Shader that reads the shader from a text file, attach, detach it etc. The problem I have is the destructor. I suppose all OpenGL objects work more or less like that

glCreateShader()
// some code here
glDeleteShader()

Lets say I design my class like this (this is just a sketch so without any details)

class Shader
{
public:

    // read from file - not important right now
    Shader( ){}

    void delete()
    { 
        glDeleteShader();
    }    

    ~Shader()
    {
        glDeleteShader();
    }
 };

I could create my object and delete it like this

Shader x;
x.delete();

but this will not delete the object itself - only frees the memory of OpenGL shader. It would be better to have something like this

Shader x;
delete(x);

where function delete() removes everything from the memory (with the object itself). Is it possible, or should I only use pointers?

Shader* x = new Shader();
delete x;

I have the same problem for any C library.

Bociek
  • 1,195
  • 2
  • 13
  • 28
  • You may find answers here: http://stackoverflow.com/questions/17161013/raii-wrapper-for-opengl-objects – roalz Jan 17 '17 at 09:42
  • @Inline I am doing it for myself just to learn more about C++ – Bociek Jan 17 '17 at 09:42
  • The destructor of objects on the stack is automatically called once they get out of scope, so you wouldn't need pointers in this case. Besides that, I'd advice to use [smart pointers](http://en.cppreference.com/w/cpp/memory/shared_ptr) when dealing with the heap, they will call the destructor for once the last pointer goes out of scope and release the accompanying memory. – Yemachu Jan 17 '17 at 09:44
  • @Bociek This could be useful https://stackoverflow.com/questions/12089859/are-there-any-plans-or-existing-projects-to-port-opengl-api-to-c – Inline Jan 17 '17 at 09:44
  • You can't delete an automatic object, but you can limit its lifetime with a scope, letting the compiler reuse its storage. Why do you want to do this, though? How is it "better"? – molbdnilo Jan 17 '17 at 09:50
  • @molbdnilo Maybe I will read the suggested posts and think once again about the proper design – Bociek Jan 17 '17 at 10:00
  • 1
    I've made [my own small wrapper in C++14](https://github.com/SuperV1234/Experiments/tree/master/emscripten/include/vrm/gl), you may find it interesting. – Vittorio Romeo Jan 17 '17 at 10:25
  • opengl itself is c-style stuff so i doubt you'll gain something. C is not that bad and you'll have less errors . – Алексей Неудачин Jan 17 '17 at 10:25
  • @АлексейНеудачин: you'll gain safety, lifetime issues caught at compile-time, genericity, expressiveness, and much more. You'll have **more** errors by using C. – Vittorio Romeo Jan 17 '17 at 10:40
  • @VittorioRomeo Thank you! I will have a look at it. I wanted to learn some object oriented design with OpenGL - that's it. I was thinking in the future that it would nice to have a class of objects like torus, cylinder, sphere ready to render on the scene rather than creating them from the scratch. I was impressed by Blender software and its pythonic interface. – Bociek Jan 17 '17 at 10:43
  • @VittorioRomeo read petzold's book.even choice bitween winapi and mfc is a challenge.but no mfc-level thing here. – Алексей Неудачин Jan 17 '17 at 12:11

1 Answers1

1

I could create my object and delete it like this

Shader x;
x.delete();

Firstly, delete is a keyword, so it may not be the name of a function.

Assuming it is OK to call glDeleteShader twice, this is OK. But the call to delete() function is redundant, since the destructor handles the call to glDeleteShader automatically. If duplicate calls to glDeleteShader is not OK, then calling delete() is not only redundant, but a bug.

Shader x;
delete(x);

where function delete() removes everything from the memory (with the object itself). Is it possible ...

It is not possible to remove an object of automatic storage duration from memory by calling a function (unless you replace the storage with a fresh object, but that's way off topic to this question). Automatic objects are removed form memory... automatically when they go out of scope.

or should I only use pointers?

No, unless you need to. If automatic objects are an option, they are the better option. Simply do this:

Shader x;
// glDeleteShader called when x is destroyed
Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326