Skip to content

Commit

Permalink
update canvas property orders to drop Device after buffers and textures
Browse files Browse the repository at this point in the history
to fix memory leak
  • Loading branch information
jonmmease committed Apr 19, 2024
1 parent 7192a42 commit e6470aa
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
41 changes: 24 additions & 17 deletions avenger-wgpu/src/canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -420,17 +420,20 @@ pub(crate) fn get_supported_sample_count(sample_flags: TextureFormatFeatureFlags
}

pub struct WindowCanvas<'window> {
window: Arc<Window>,
surface: Surface<'window>,
device: Device,
queue: Queue,
multisampled_framebuffer: TextureView,
sample_count: u32,
surface_config: SurfaceConfiguration,
dimensions: CanvasDimensions,
marks: Vec<MarkRenderer>,
multi_renderer: Option<MultiMarkRenderer>,
config: CanvasConfig,

// Order of properties determines drop order.
// Device must be dropped after the buffers and textures associated with marks
multisampled_framebuffer: TextureView,
queue: Queue,
device: Device,
surface: Surface<'window>,
window: Arc<Window>,
}

impl<'window> WindowCanvas<'window> {
Expand Down Expand Up @@ -623,20 +626,24 @@ impl<'window> Canvas for WindowCanvas<'window> {
}

pub struct PngCanvas {
device: Device,
queue: Queue,
multisampled_framebuffer: TextureView,
sample_count: u32,
marks: Vec<MarkRenderer>,
pub dimensions: CanvasDimensions,
pub texture_view: TextureView,
pub output_buffer: Buffer,
pub texture: Texture,
pub texture_size: Extent3d,
pub padded_width: u32,
pub padded_height: u32,
pub multi_renderer: Option<MultiMarkRenderer>,
pub config: CanvasConfig,
dimensions: CanvasDimensions,
texture_view: TextureView,
output_buffer: Buffer,
texture: Texture,
texture_size: Extent3d,
padded_width: u32,
padded_height: u32,
multi_renderer: Option<MultiMarkRenderer>,
config: CanvasConfig,

// The order of properties in a struct is the order in which items are dropped.
// wgpu seems to require that the device be dropped last, otherwise there is a resouce
// leak.
multisampled_framebuffer: TextureView,
queue: Queue,
device: Device,
}

impl PngCanvas {
Expand Down
11 changes: 7 additions & 4 deletions avenger-wgpu/src/html_canvas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,19 @@ use wgpu::{
};

pub struct HtmlCanvasCanvas<'window> {
surface: Surface<'window>,
device: Device,
queue: Queue,
multisampled_framebuffer: TextureView,
sample_count: u32,
surface_config: SurfaceConfiguration,
dimensions: CanvasDimensions,
marks: Vec<MarkRenderer>,
multi_renderer: Option<MultiMarkRenderer>,
config: CanvasConfig,

// The order of properties determines that drop order and device must be dropped after
// the buffers and textures associated with marks.
multisampled_framebuffer: TextureView,
queue: Queue,
device: Device,
surface: Surface<'window>,
}

impl<'window> HtmlCanvasCanvas<'window> {
Expand Down

0 comments on commit e6470aa

Please sign in to comment.