gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-29 08:34:08 24 4
gpt4 key购买 nike

我正在尝试从 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com