3

I'm exploring OpenGL with Qt6, and I came across some of their examples. The thing that draws my attention from a C++ perspective is the following piece of code.

delete m_program; // <-- is this necessary before creating m_program?
m_program = new QOpenGLShaderProgram;
m_program->addShaderFromSourceCode(QOpenGLShader::Vertex, vertexShaderSource);
m_program->addShaderFromSourceCode(QOpenGLShader::Fragment, fragmentShaderSource);
m_program->link();

// Create a VAO. Not strictly required for ES 3, but it is for plain OpenGL.
delete m_vao; // <-- is this necessary?
m_vao = new QOpenGLVertexArrayObject;
if (m_vao->create())
    m_vao->bind();

m_program->bind();
delete m_vbo; // <-- is this necessary?
m_vbo = new QOpenGLBuffer;
m_vbo->create();
m_vbo->bind();

where

QOpenGLShaderProgram *m_program = nullptr;
QOpenGLBuffer *m_vbo = nullptr;
QOpenGLVertexArrayObject *m_vao = nullptr;

What is the point behind this practice? Why does one possibly need to delete nullptr before creating the object? Please note that in the code, these objects have not been created yet when the delete is called.

7
  • 6
    delete with a null pointer does nothing, so it's OK, but totally unnecessary. See e.g. here. Maybe the code is written like that because there are cases where the pointers are not null (and then it's a must to delete them to avoid a memory leak). Commented Oct 10 at 3:21
  • 3
    these objects have not been created yet when the delete is called --- this is a wrong assumption for a public member function. Commented Oct 10 at 3:25
  • 1
    Using smart pointer would do it under the hood too in the assignation. Commented Oct 10 at 7:10
  • What makes you so sure that the three pointers are all null as you claim? In that code, inititializeGL() is a public member that can be called at any time during the object's life. Commented Oct 10 at 8:32
  • @TobySpeight Well, these pointers are private data members and initialised with nullptr in the class body. Commented Oct 10 at 18:52

1 Answer 1

11

TL;DR:

The pointers are deleted because they might not be null in certain scenarios.
And if they are null in a specific scenario - deleteing them is OK because it does nothing.

More info:

If indeed the pointers are guaranteed to be null, then deleteing them before a new allocation is OK (delete with a null pointer does nothing - see e.g. here) but totally unnecessary.

But it seems like in this case the pointers are not guaranteed to be null.

For example this is done in GLWindow::initializeGL(), which might be called multiple times.

In the first time the pointers will be null, but in the next times they will not, and then it is a must to delete before reallocating them to avoid a memory leak.

Since as mentioned above deleteing a null pointer does nothing, the author of the code decided (correctly) it is not necessary to check for null.

Note that for example in the constructor GLWindow::GLWindow() pointers are not deleted before allocation because the author presumebaly understood there is no need.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you. That is a very insightful response. May I inquire why such a function, which is expected to be invoked only once, is being called multiple times?
I am actually not familiar with the API, so I cannot tell for sure. I simply reviewed some of the code examples you linked and explained what I saw. I can only guess that opengl context can be re-initialized in certain cases.
The docs are a bit ambiguous indeed, as the phrase "once before the first call to paintGL() or resizeGL()" could be interpreted as both "before either one of them gets called first" or "once each of them gets called the first time". With the help of codebrowser.dev it seems like it may be called more than once, so it's possible that that deletion has been added for extra safety. Yet, consider that examples don't usually get the same attention as the codebase does, so it's possible that those were put in earlier Qt versions and never removed, or they're simply there by mistake.
With newer QOpenGLWindow\QOpenGLWidget that somehow can happen, depending paltform. Their design looks a bit weird until we remeber that framework is meant for platform very different in how they handle OpenGL.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.