QOpenGLContext memory leak



  • Hi everyone,

    I've got this problem where deleting my OpenGLContext does not work. The VRAM increases with every new creation of MyWindow. The context is created in another Thread with following code.

    if (void* context = MakeContext())
    {
    	return std::shared_ptr<void>(context, [this](void* cxt) { DeleteContext(cxt); });
    }
    
    void* MyWindow::MakeContext()
    {
    	auto context = new QOpenGLContext();
    	context->setFormat(m_format);
    	context->create();
    	return context;
    }
    
    void MyWindow::DeleteContext(void* context)
    {
    	((QOpenGLContext*)context)->deleteLater();
    }
    

    Using following method results in a random crash on new Creation of MyWindow after the first time.
    Yet it fixes the VRAM issue.

    void MyWindow::DeleteContext(void* context)
    {
    	delete (QOpenGLContext*)context;
    }
    

    What am I doing wrong?

    Edit:
    I noticed it crashes on new context creation.

    void* MyWindow::MakeContext()
    {
    	auto context = new QOpenGLContext();
    	context->setFormat(m_format);
    	context->create(); // this line crashes
    	return context;
    }
    


  • Somehow this seems to have fixed it to some extend. But I have no clue why and if it is the best solution.

    void MyWindow::DeleteContext(void* context)
    {
    	QOpenGLContext* trash = ((QOpenGLContext*)context);
    	trash->deleteLater();
    	trash->~QOpenGLContext();
    }
    

  • Lifetime Qt Champion

    Hi,

    You should use qobject_cast rather that C style cast. Then calling the destructor is usually not something you do. You should simply replace deleteLater for delete if you want to delete everything immediately.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.