Skip to content

Commit f5c5c1a

Browse files
committed
Fix leaking swapchain textures
1 parent 9847cc4 commit f5c5c1a

File tree

1 file changed

+15
-7
lines changed

1 file changed

+15
-7
lines changed

src/device.rs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@ use crate::{
1515
};
1616

1717
use back;
18+
use hal::backend::FastHashMap;
1819
use hal::command::RawCommandBuffer;
1920
use hal::queue::RawCommandQueue;
20-
use hal::{self,
21+
use hal::{
22+
self,
2123
DescriptorPool as _DescriptorPool,
2224
Device as _Device,
2325
Surface as _Surface,
@@ -27,7 +29,7 @@ use log::{info, trace};
2729
use parking_lot::{Mutex};
2830

2931
use std::{ffi, iter, slice};
30-
use std::collections::hash_map::{Entry, HashMap};
32+
use std::collections::hash_map::Entry;
3133
use std::sync::atomic::Ordering;
3234

3335

@@ -64,7 +66,7 @@ pub(crate) struct FramebufferKey {
6466
}
6567
impl Eq for FramebufferKey {}
6668

67-
#[derive(Debug)]
69+
#[derive(Debug, PartialEq)]
6870
enum ResourceId {
6971
Buffer(BufferId),
7072
Texture(TextureId),
@@ -100,6 +102,7 @@ unsafe impl<B: hal::Backend> Sync for DestroyedResources<B> {}
100102

101103
impl<B: hal::Backend> DestroyedResources<B> {
102104
fn add(&mut self, resource_id: ResourceId, ref_count: RefCount) {
105+
debug_assert!(!self.referenced.iter().any(|r| r.0 == resource_id));
103106
self.referenced.push((resource_id, ref_count));
104107
}
105108

@@ -195,8 +198,8 @@ pub struct Device<B: hal::Backend> {
195198
life_guard: LifeGuard,
196199
pub(crate) trackers: Mutex<TrackerSet>,
197200
mem_props: hal::MemoryProperties,
198-
pub(crate) render_passes: Mutex<HashMap<RenderPassKey, B::RenderPass>>,
199-
pub(crate) framebuffers: Mutex<HashMap<FramebufferKey, B::Framebuffer>>,
201+
pub(crate) render_passes: Mutex<FastHashMap<RenderPassKey, B::RenderPass>>,
202+
pub(crate) framebuffers: Mutex<FastHashMap<FramebufferKey, B::Framebuffer>>,
200203
desc_pool: Mutex<B::DescriptorPool>,
201204
destroyed: Mutex<DestroyedResources<B>>,
202205
}
@@ -269,8 +272,8 @@ impl<B: hal::Backend> Device<B> {
269272
life_guard,
270273
trackers: Mutex::new(TrackerSet::new()),
271274
mem_props,
272-
render_passes: Mutex::new(HashMap::new()),
273-
framebuffers: Mutex::new(HashMap::new()),
275+
render_passes: Mutex::new(FastHashMap::default()),
276+
framebuffers: Mutex::new(FastHashMap::default()),
274277
desc_pool,
275278
destroyed: Mutex::new(DestroyedResources {
276279
referenced: Vec::new(),
@@ -1380,7 +1383,12 @@ pub fn device_create_swap_chain(
13801383

13811384
let (old_raw, sem_available, command_pool) = match surface.swap_chain.take() {
13821385
Some(mut old) => {
1386+
let mut destroyed = device.destroyed.lock();
13831387
assert_eq!(old.device_id.value, device_id);
1388+
for frame in old.frames {
1389+
destroyed.add(ResourceId::Texture(frame.texture_id.value), frame.texture_id.ref_count);
1390+
destroyed.add(ResourceId::TextureView(frame.view_id.value), frame.view_id.ref_count);
1391+
}
13841392
unsafe {
13851393
old.command_pool.reset()
13861394
};

0 commit comments

Comments
 (0)