首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么从Rust和Glium调用XChangeProperty会产生段错误?

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

Stack Overflow用户
提问于 2017-10-29 23:24:17
回答 1查看 294关注 0票数 0

我试图从Rust发出一个xlib调用,但我不明白为什么这段代码会给我一个段错误。

main.rs:

代码语言:javascript
复制
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:

代码语言:javascript
复制
[package]
name = "rust-xlib"
version = "0.1.0"
authors = ["Me <me@me.me>"]

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

Valgrind报告的分段故障是:

代码语言:javascript
复制
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参数似乎有问题,但我真的不知道它可能有什么问题。

EN

回答 1

Stack Overflow用户

发布于 2017-10-31 19:00:54

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

代码语言:javascript
复制
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,
    );
}
票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47001895

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档