#4989·windows-rs

`windows-reactor`: 由新的 `Image::source_data` 取代的位图解码从未被释放

作者: Tam1SH创建于 2026年9月28日更新于 2026年9月28日
标签bug

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

use windows_reactor::*;

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

static PROFILER: Mutex<Optiondhat::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() { …

内容来源: microsoft/windows-rs