mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-10-24 09:46:59 -06:00
Merge pull request #3749 from Silvertree2010/fix/3598-audit-rendering-depth-counters-a
Restore render depth counters after a caught panic
This commit is contained in:
@@ -734,6 +734,7 @@ impl Renderer {
|
||||
/// Start a frame: cache entries used from now on are marked with its stamp.
|
||||
fn begin_frame(&mut self) {
|
||||
self.stamp += 1;
|
||||
self.inline_depth = 0;
|
||||
}
|
||||
|
||||
/// End a frame: drop cache entries not used for a few frames (every render, thumbnails too,
|
||||
|
||||
@@ -205,10 +205,18 @@ fn with_tile_renderer<T>(f: impl FnOnce(&mut Renderer) -> T) -> Option<T> {
|
||||
}))
|
||||
})?;
|
||||
let mut r = r;
|
||||
// Restores the depth even if `f` panics and the panic is caught (the renderer is then dropped).
|
||||
struct Restore;
|
||||
impl Drop for Restore {
|
||||
fn drop(&mut self) {
|
||||
CACHE.with(|c| c.borrow_mut().depth -= 1);
|
||||
}
|
||||
}
|
||||
let guard = Restore;
|
||||
let out = f(&mut r);
|
||||
drop(guard);
|
||||
CACHE.with(|c| {
|
||||
let mut c = c.borrow_mut();
|
||||
c.depth -= 1;
|
||||
if c.renderer.is_none() {
|
||||
c.renderer = Some(r);
|
||||
}
|
||||
@@ -687,4 +695,13 @@ mod tests {
|
||||
assert_eq!(raster_scale(2.1, (10.0, 10.0)), raster_scale(3.9, (10.0, 10.0)));
|
||||
assert!(raster_scale(64.0, (1000.0, 10.0)) * 1000.0 <= MAX_TILE_PX);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn tile_renderer_depth_survives_caught_panics() {
|
||||
for _ in 0..4 {
|
||||
let r = std::panic::catch_unwind(|| with_tile_renderer(|_| panic!("tile boom")));
|
||||
assert!(r.is_err());
|
||||
}
|
||||
assert!(with_tile_renderer(|_| ()).is_some(), "depth leaked after caught panics");
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user