From 9c38b0fdbe32dc2cb81d53c9be90113d114f1cd2 Mon Sep 17 00:00:00 2001 From: Tom Englund Date: Mon, 29 Jul 2024 19:19:47 +0200 Subject: core: add a destructor to CHyprOpenglImpl and avoid wl_container_of undefined behaviour (#7101) * protocols: avoid undefined behaviour in C macro to safely use wl_container_of with a class the class has to be no virtual functions, no inheritance, and uniform access control (e.g all public) work around this by putting this into a destroywrapper struct. * opengl: clean memory on destruction add a destructor and free the allocated memory and close the fd --- src/render/OpenGL.cpp | 16 ++++++++++++++++ src/render/OpenGL.hpp | 1 + 2 files changed, 17 insertions(+) (limited to 'src/render') diff --git a/src/render/OpenGL.cpp b/src/render/OpenGL.cpp index 43a2c3a7..b925fcc9 100644 --- a/src/render/OpenGL.cpp +++ b/src/render/OpenGL.cpp @@ -345,6 +345,22 @@ CHyprOpenGLImpl::CHyprOpenGLImpl() { m_tGlobalTimer.reset(); } +CHyprOpenGLImpl::~CHyprOpenGLImpl() { + if (m_pEglDisplay && m_pEglContext != EGL_NO_CONTEXT) + eglDestroyContext(m_pEglDisplay, m_pEglContext); + + if (m_pEglDisplay) + eglTerminate(m_pEglDisplay); + + eglReleaseThread(); + + if (m_pGbmDevice) + gbm_device_destroy(m_pGbmDevice); + + if (m_iGBMFD >= 0) + close(m_iGBMFD); +} + std::optional> CHyprOpenGLImpl::getModsForFormat(EGLint format) { // TODO: return std::expected when clang supports it diff --git a/src/render/OpenGL.hpp b/src/render/OpenGL.hpp index 41e80ee5..1e8325c1 100644 --- a/src/render/OpenGL.hpp +++ b/src/render/OpenGL.hpp @@ -145,6 +145,7 @@ class CGradientValueData; class CHyprOpenGLImpl { public: CHyprOpenGLImpl(); + ~CHyprOpenGLImpl(); void begin(CMonitor*, const CRegion& damage, CFramebuffer* fb = nullptr, std::optional finalDamage = {}); void beginSimple(CMonitor*, const CRegion& damage, SP rb = nullptr, CFramebuffer* fb = nullptr); -- cgit v1.2.3