我的 Xlib 包装类中的分段错误 (C++)

发布于 2024-10-17 08:17:09 字数 585 浏览 1 评论 0原文

我正在尝试为 Xlib 库创建一个包装类,但出现分段错误!我是 C++ 新手,这可能超出了我的深度,也许我的目标设置得很高,除了这个问题之外,有人可以告诉我为什么会出现此分段错误吗?
源文件
头文件
main.cpp

我相信这是我得到的回溯:

Program received signal SIGSEGV, Segmentation fault.
In XMapWindow () (/usr/lib/libX11.so.6)
At /home/elliot/Programming/C and C++/XWindows/src/MyWindow.cpp:49

I am attempting to create a wrapper class to the Xlib library, but I get a segmentation fault! I am new to C++ and this is probably out of my depth and maybe have my goals set to high, other than that problem, can someone tell my why I'm getting this segmentation fault?
source file
header file
main.cpp

I believe this is the back trace I get:

Program received signal SIGSEGV, Segmentation fault.
In XMapWindow () (/usr/lib/libX11.so.6)
At /home/elliot/Programming/C and C++/XWindows/src/MyWindow.cpp:49

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

乖乖哒 2024-10-24 08:17:09

您的错误表明 XMapWindow()displaywindow 参数不正确。

您需要在代码中包含更多错误检查,特别是针对 XOpenDisplayXCreateWindow 调用的结果。

我看到的唯一明显的错误是您将 CWBackPixel 标志传递给 XCreateWindow ,但未初始化属性参数。与普通 C 不同,C++ 在声明结构时不会清除结构的内存内容。

编辑 - 我当然错过了真正令人眼花缭乱的明显错误 - 你无意中在构造函数中重新声明了所有类成员变量。这将是你的范围问题。您需要从构造函数内的所有赋值中删除类型名,例如:

MyWindow::MyWindow()
{   //ctor
    display = XOpenDisplay(NULL);
    visual = XDefaultVisual(display, 0);
    depth = XDefaultDepth(display, 0);
    window = XCreateWindow(display, XRootWindow(display, 0), 0, 0, MyWindow::default_width, MyWindow::default_height, 16, depth, InputOutput, visual, CWBackPixel, &attributes);
    XStoreName(display, window, MyWindow::default_caption.c_str());
    XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask);
}

Your error indicates that either the display or window parameters to XMapWindow() were incorrect.

You need to include more error checking in your code, in particular for the results of the calls to XOpenDisplay and XCreateWindow.

The only obvious error I can see is that you're passing the CWBackPixel flag to XCreateWindow but have left the attributes parameter uninitialised. Unlike plain C, C++ does not clear the memory contents of structures when they're declared.

EDIT - I of course missed the really blindingly obvious error - you've inadvertently redeclared all of your classes member variables within your constructor. That'll be your scope problem. You need to remove the typenames from all of the assignments within the constructor, e.g.:

MyWindow::MyWindow()
{   //ctor
    display = XOpenDisplay(NULL);
    visual = XDefaultVisual(display, 0);
    depth = XDefaultDepth(display, 0);
    window = XCreateWindow(display, XRootWindow(display, 0), 0, 0, MyWindow::default_width, MyWindow::default_height, 16, depth, InputOutput, visual, CWBackPixel, &attributes);
    XStoreName(display, window, MyWindow::default_caption.c_str());
    XSelectInput(display, window, ExposureMask | KeyPressMask | KeyReleaseMask);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文