Fix memory leak in iced_wgpu::Buffer

I know, I know... Skill issue.
This commit is contained in:
Héctor Ramón Jiménez 2025-10-25 19:26:54 +02:00
parent 26dfcb6d42
commit 92888a3639
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 30 additions and 39 deletions

View file

@ -13,7 +13,6 @@ pub struct Buffer<T> {
size: u64,
usage: wgpu::BufferUsages,
pub(crate) raw: wgpu::Buffer,
offsets: Vec<wgpu::BufferAddress>,
type_: PhantomData<T>,
}
@ -38,7 +37,6 @@ impl<T: bytemuck::Pod> Buffer<T> {
size,
usage,
raw,
offsets: Vec::new(),
type_: PhantomData,
}
}
@ -47,8 +45,6 @@ impl<T: bytemuck::Pod> Buffer<T> {
let new_size = (std::mem::size_of::<T>() * new_count) as u64;
if self.size < new_size {
self.offsets.clear();
self.raw = device.create_buffer(&wgpu::BufferDescriptor {
label: Some(self.label),
size: new_size,
@ -108,8 +104,6 @@ impl<T: bytemuck::Pod> Buffer<T> {
)
.copy_from_slice(&bytes[bytes_written..]);
self.offsets.push(offset as u64);
bytes.len()
}
@ -120,19 +114,11 @@ impl<T: bytemuck::Pod> Buffer<T> {
self.raw.slice(bounds)
}
/// Returns the slice calculated from the offset stored at the given index.
pub fn slice_from_index(&self, index: usize) -> wgpu::BufferSlice<'_> {
self.raw.slice(self.offset_at(index)..)
}
/// Clears any temporary data (i.e. offsets) from the buffer.
pub fn clear(&mut self) {
self.offsets.clear();
}
/// Returns the offset at `index`, if it exists.
fn offset_at(&self, index: usize) -> &wgpu::BufferAddress {
self.offsets.get(index).expect("No offset at index.")
pub fn range(&self, start: usize, end: usize) -> wgpu::BufferSlice<'_> {
self.slice(
start as u64 * std::mem::size_of::<T>() as u64
..end as u64 * std::mem::size_of::<T>() as u64,
)
}
}