rust-windowing / rust-windowing/winit

Window only destroyed when dropped inside event loop

Open
#2,345 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

B - bug DS - x11
Dominant language
Rust
Stars
6.2k
Forks
1.3k
Avg merge
2d 19h
Merged PRs (30d)
9

Description

Running on linux/x11, the following code:

use winit::{
    event::{Event, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    platform::run_return::EventLoopExtRunReturn,
    window::Window,
};

fn main() {
    let mut event_loop = EventLoop::new();
    {
        let window = Window::new(&event_loop);

        event_loop.run_return(|event, _, control_flow| match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => *control_flow = ControlFlow::Exit,
            _ => (),
        });

        println!("dropping window");
    }

    loop {}
}

Causes the window to stay open after it is dropped.
But changing it to this fixes it:

use winit::{
    event::{Event, WindowEvent},
    event_loop::{ControlFlow, EventLoop},
    platform::run_return::EventLoopExtRunReturn,
    window::Window,
};

fn main() {
    let mut event_loop = EventLoop::new();
    {
        let mut window = Some(Window::new(&event_loop));

        event_loop.run_return(|event, _, control_flow| match event {
            Event::WindowEvent {
                event: WindowEvent::CloseRequested,
                ..
            } => {
                let _ = window.take();
                *control_flow = ControlFlow::Exit;
                println!("dropping window");
            }
            _ => (),
        });
    }

    loop {}
}

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the Linux/X11 example using EventLoopExtRunReturn, EventLoop, and Window, then trace how run_return handles destruction after ControlFlow::Exit. Compare the direct drop with the window.take() variant. Done means dropping the window after the event loop returns closes it without requiring destruction inside the callback.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
desktop
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.