rust - 为什么从 Rust 和 Glium 调用 XChangeProperty 会产生段错误?

标签 rust x11 xlib glium

我正在尝试从 Rust 进行 xlib 调用,但我无法弄清楚为什么这段代码会给我一个段错误。

主要.rs:

extern crate glium;
extern crate x11;

fn main() {
    let mut events_loop = glium::glutin::EventsLoop::new();
    let context = glium::glutin::ContextBuilder::new();
    let window = glium::glutin::WindowBuilder::new()
        .with_dimensions(800, 600)
        .with_title("Backr");
    let display = glium::Display::new(window, context, &events_loop).unwrap();

    unsafe {
        use x11::xlib;

        let x_display = display.gl_window().platform_display() as *mut xlib::Display;
        let x_window = display.gl_window().platform_window() as u64;
        let x_type = CStr::from_bytes_with_nul(b"_NET_WM_WINDOW_TYPE\0").unwrap();
        let x_value = CStr::from_bytes_with_nul(b"_NET_WM_WINDOW_TYPE_DESKTOP\0").unwrap();
        xlib::XChangeProperty(
            x_display,
            x_window,
            xlib::XInternAtom(x_display, x_type.as_ptr(), xlib::False),
            xlib::XA_ATOM,
            32,
            xlib::PropModeReplace,
            xlib::XInternAtom(x_display, x_type.as_ptr(), xlib::False) as *const u8,
            1,
        );
    }
}

cargo .toml:

[package]
name = "rust-xlib"
version = "0.1.0"
authors = ["Me <me@me.me>"]

[dependencies]
glium = "*" 
x11 = { version = "*", features = ["xlib"] }

Valgrind 报告的段错误是:

Invalid read of size 8
   at 0x4E7DC40: _XData32 (in /usr/lib/libX11.so.6.3.0)
   by 0x4E58682: XChangeProperty (in /usr/lib/libX11.so.6.3.0)
   by 0x126898: rust-xlib::main (main.rs:17)
   by 0x362397: __rust_maybe_catch_panic (in /home/me/dev/rust-xlib/target/debug/rust-xlib)      
   by 0x34F847: std::panicking::try (in /home/me/dev/rust-xlib/target/debug/rust-xlib)
   by 0x34C325: std::rt::lang_start (in /home/me/dev/rust-xlib/target/debug/rust-xlib)
   by 0x1269F2: main (in /home/me/dev/rust-xlib/target/debug/rust-xlib)
 Address 0x188 is not stack'd, malloc'd or (recently) free'd


Process terminating with default action of signal 11 (SIGSEGV): dumping core
 Access not within mapped region at address 0x18A
   at 0x4E7DC40: _XData32 (in /usr/lib/libX11.so.6.3.0)
   by 0x4E58682: XChangeProperty (in /usr/lib/libX11.so.6.3.0)
   by 0x126898: rust-xlib::main (main.rs:17) 
   by 0x362397: __rust_maybe_catch_panic (in /home/me/dev/rust-xlib/target/debug/rust-xlib)      
   by 0x34F847: std::panicking::try (in /home/me/dev/rust-xlib/target/debug/rust-xlib)
   by 0x34C325: std::rt::lang_start (in /home/me/dev/rust-xlib/target/debug/rust-xlib)
   by 0x1269F2: main (in /home/me/dev/rust-xlib/target/debug/rust-xlib)

从错误消息看来,XChangePropertydata 参数似乎有问题,但我真的不知道它可能有什么问题。

最佳答案

这应该是正确的解决方案:

unsafe {
    use x11::xlib;

    let x_display = display.gl_window().platform_display() as *mut xlib::Display;
    let x_window = display.gl_window().platform_window() as u64;
    let x_type = CStr::from_bytes_with_nul(b"_NET_WM_WINDOW_TYPE\0").unwrap();
    let x_value = CStr::from_bytes_with_nul(b"_NET_WM_WINDOW_TYPE_DESKTOP\0").unwrap();
    let x_data = xlib::XInternAtom(x_display, x_value.as_ptr(), xlib::False);
    xlib::XChangeProperty(
        x_display,
        x_window,
        xlib::XInternAtom(x_display, x_type.as_ptr(), xlib::False),
        xlib::XA_ATOM,
        32,
        xlib::PropModeReplace,
        std::mem::transmute(&x_data),
        1,
    );
}

关于rust - 为什么从 Rust 和 Glium 调用 XChangeProperty 会产生段错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47001895/

相关文章:

c - 无法使用 XFreeColors() 释放 Xlib 中的颜色图条目

C++ 和 XGrabKeyboard 键盘锁定

rust - 找不到关联类型的特征实现

generics - 闭包作为 Rust 结构中的一种类型

linux - 以其他用户身份运行 X Apps 时无法打开显示

linux - 如果 "pure XCB"OpenGL 是不可能的,那么 xcb/glx.h 中的 XCB/GLX API 有什么用?

c - 为什么我不能从我的循环中捕获这些 KeyPress/KeyRelease 事件?

python - 禁用键盘但继续获取事件

random - 如何在 Rust 中生成一个范围内的随机数?

arrays - 初始化固定长度数组的正确方法是什么?