Clean up glow painter destroy code

This commit is contained in:
Emil Ernerfeldt 2021-12-28 14:24:59 +01:00
parent beb2ea2ef6
commit 5b9a349c26
2 changed files with 15 additions and 12 deletions

View file

@ -48,7 +48,7 @@ pub struct Painter {
/// Stores outdated OpenGL textures that are yet to be deleted /// Stores outdated OpenGL textures that are yet to be deleted
textures_to_destroy: Vec<glow::Texture>, textures_to_destroy: Vec<glow::Texture>,
/// Only used in debug builds, to make sure we are destroyed correctly. /// Used to make sure we are destroyed correctly.
destroyed: bool, destroyed: bool,
} }
@ -452,18 +452,19 @@ impl Painter {
/// that should be deleted. /// that should be deleted.
pub fn destroy(&mut self, gl: &glow::Context) { pub fn destroy(&mut self, gl: &glow::Context) {
debug_assert!(!self.destroyed, "Only destroy once!"); if !self.destroyed {
unsafe { unsafe {
self.destroy_gl(gl); self.destroy_gl(gl);
if let Some(ref post_process) = self.post_process { if let Some(ref post_process) = self.post_process {
post_process.destroy(gl); post_process.destroy(gl);
}
} }
self.destroyed = true;
} }
self.destroyed = true;
} }
fn assert_not_destroyed(&self) { fn assert_not_destroyed(&self) {
debug_assert!(!self.destroyed, "the egui glow has already been destroyed!"); assert!(!self.destroyed, "the egui glow has already been destroyed!");
} }
} }
@ -486,10 +487,11 @@ pub fn clear(gl: &glow::Context, dimension: [u32; 2], clear_color: egui::Rgba) {
impl Drop for Painter { impl Drop for Painter {
fn drop(&mut self) { fn drop(&mut self) {
debug_assert!( if !self.destroyed {
self.destroyed, eprintln!(
"Make sure to call destroy() before dropping to avoid leaking OpenGL objects!" "You forgot to call destroy() on the egui glow painter. Resources will leak!"
); );
}
} }
} }

View file

@ -221,6 +221,7 @@ impl PostProcess {
gl.bind_texture(glow::TEXTURE_2D, None); gl.bind_texture(glow::TEXTURE_2D, None);
gl.use_program(None); gl.use_program(None);
} }
pub(crate) unsafe fn destroy(&self, gl: &glow::Context) { pub(crate) unsafe fn destroy(&self, gl: &glow::Context) {
gl.delete_buffer(self.pos_buffer); gl.delete_buffer(self.pos_buffer);
gl.delete_buffer(self.index_buffer); gl.delete_buffer(self.index_buffer);