From f890b4011d0daa964a1b43872decebc3ecc89be7 Mon Sep 17 00:00:00 2001 From: Drakulix Date: Fri, 2 Jun 2017 15:25:16 +0200 Subject: [PATCH] Fix egl initialization segfaults - Don't initialize a surface twice, if context creation fails for one version - Don't let the loaded egl library go out of scope and thus invalidating the function pointers --- src/backend/graphics/egl.rs | 34 ++++++++++++++++++---------------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/src/backend/graphics/egl.rs b/src/backend/graphics/egl.rs index 4e393fa..4a37441 100644 --- a/src/backend/graphics/egl.rs +++ b/src/backend/graphics/egl.rs @@ -117,10 +117,11 @@ impl error::Error for CreationError { /// EGL context for rendering pub struct EGLContext { - context: *const c_void, - display: *const c_void, + _lib: Library, + context: ffi::egl::types::EGLContext, + display: ffi::egl::types::EGLDisplay, egl: ffi::egl::Egl, - surface: *const c_void, + surface: ffi::egl::types::EGLSurface, pixel_format: PixelFormat, } @@ -384,19 +385,6 @@ impl EGLContext { srgb: false, // TODO: use EGL_KHR_gl_colorspace to know that }; - let surface = { - let surface = match native { - Native::X11(_, window) | - Native::Wayland(_, window) | - Native::Gbm(_, window) => egl.CreateWindowSurface(display, config_id, window, ptr::null()), - }; - - if surface.is_null() { - return Err(CreationError::OsError(String::from("eglCreateWindowSurface failed"))); - } - surface - }; - let mut context_attributes = Vec::with_capacity(10); let mut flags = 0; @@ -498,7 +486,21 @@ impl EGLContext { } } + let surface = { + let surface = match native { + Native::X11(_, window) | + Native::Wayland(_, window) | + Native::Gbm(_, window) => egl.CreateWindowSurface(display, config_id, window, ptr::null()), + }; + + if surface.is_null() { + return Err(CreationError::OsError(String::from("eglCreateWindowSurface failed"))); + } + surface + }; + Ok(EGLContext { + _lib: lib, context: context as *const _, display: display as *const _, egl: egl,