#4989·windows-rs

`windows-reactor`: a bitmap decode superseded by a new `Image::source_data` is never freed

Author: Tam1SHCreated Sep 28, 2026Updated Sep 28, 2026
Labelsbug

Summary

Changing an Image's EncodedImage while its SetSourceAsync is still running leaks that decode's completion plumbing: 144 B per change, forever.

Repro on a38689a (attached): one Image, a 2048×2048 PNG swapped 1000 times every 4 ms, dhat dumped 5 s after the last swap.

dhat: At t-end:  196,508 bytes in 3,084 blocks

1000 of 1001 blocks kept at each of, all from start_bitmap_decode:
  72 B  windows_future::Async::when (AsyncActionCompletedHandler)   mod.rs:4550
  48 B  Arc::new in background_dispatch_waker                        mod.rs:4527
  24 B  background_dispatch_waker                                    mod.rs:4527

dhat-heap.json

Crate manifest

toml
[package]
name = "reactor-image-leak"
version = "0.0.0"
edition = "2024"
publish = false

[dependencies]
windows-reactor = { git = "https://github.com/microsoft/windows-rs", rev = "a38689a8b" }
dhat = "0.3"
png = "0.18"

[build-dependencies]
windows-reactor-setup = { git = "https://github.com/microsoft/windows-rs", rev = "a38689a8b" }

[profile.release]
debug = true

Crate code

rust
use std::sync::Mutex;
use std::time::Duration;

use windows_reactor::*;

#[global_allocator]
static GLOBAL: dhat::Alloc = dhat::Alloc;

static PROFILER: Mutex<Option<dhat::Profiler>> = Mutex::new(None);

const SWAPS: u32 = 1000;
const SIDE: u32 = 2048;

fn png(rgba: [u8; 4]) -> EncodedImage {
    let mut bytes = Vec::new();
    let mut encoder = png::Encoder::new(&mut bytes, SIDE, SIDE);
    encoder.set_color(png::ColorType::Rgba);
    encoder.set_depth(png::BitDepth::Eight);
    let pixels = rgba.repeat((SIDE * SIDE) as usize);
    encoder.write_header().unwrap().write_image_data(&pixels).unwrap();
    EncodedImage::new(bytes)
}

struct Repro {
    swaps: u32,
    red: EncodedImage,
    blue: EncodedImage,
}

impl Component for Repro {
    type Message = ();
    type Input = ();

    fn create(_input: &(), context: &ComponentContext<Self>) -> Self {
        context.spawn_background(|_| std::thread::sleep(Duration::from_millis(4)));
        Self {
            swaps: 0,
            red: png([220, 40, 40, 255]),
            blue: png([40, 120, 220, 255]),
        }
    }

    fn update(&mut self, _message: (), context: &ComponentContext<Self>) {
        if self.swaps < SWAPS {
            self.swaps += 1;
            context.spawn_background(|_| std::thread::sleep(Duration::from_millis(4)));
        } else {
            context.spawn_background(|_| {
                std::thread::sleep(Duration::from_secs(5));
                drop(PROFILER.lock().unwrap().take());
                std::process::exit(0);
            });
        }
    }

    fn view(&self, _input: &Self::Input, _context: &mut ViewContext<Self>) -> View {
        let source = if self.swaps % 2 == 0 { &self.red } else { &self.blue };
        Image::new().width(64.0).height(64.0).source_data(source.clone()).into()
    }
}

fn main() {
    *PROFILER.lock().unwrap() = Some(dhat::Profiler::builder().trim_backtraces(None).build());
    App::run_component::<Repro>(()).unwrap();
}