XQueryTree上的分段错误
Segmentation fault on XQueryTree
我正在尝试关闭上次使用的窗口(堆叠顺序中当前窗口正下方的窗口)。不幸的是,XQueryTree由于某种原因出现了段错误。
#pragma once
#include <X11/Xlib.h>
#include <X11/Xutil.h>
namespace WindowingOperations {
inline void closeLastWindow() {
Display* dpy = XOpenDisplay(0);
Window root = DefaultRootWindow(dpy);
Window* root_return;
Window* parent_return;
Window** children_return;
unsigned int* nchildren_return;
XQueryTree(dpy,
root,
root_return,
parent_return,
children_return,
nchildren_return);
// Kill the window right after this one
if (*nchildren_return > 1)
XDestroyWindow(dpy, *children_return[*nchildren_return - 2]);
}
}
编辑:
如果您需要测试用例:
#include "window_operations.h"
int main() {
WindowingOperations::closeLastWindow();
return 0;
}
_return
参数需要去某个地方。您不能只传入未初始化的指针,需要为XQueryTree
分配存储空间以写入结果。
所以。。。
namespace WindowingOperations {
inline void closeLastWindow() {
Display* dpy = XOpenDisplay(0);
Window root = DefaultRootWindow(dpy);
// Allocate storage for the results of XQueryTree.
Window root_return;
Window parent_return;
Window* children_return;
unsigned int nchildren_return;
// then make the call providing the addresses of the out parameters
if (XQueryTree(dpy,
root,
&root_return,
&parent_return,
&children_return,
&nchildren_return) != 0)
{ // added if to test for a failed call. results are unchanged if call failed,
// so don't use them
// Kill the window right after this one
if (*nchildren_return > 1)
XDestroyWindow(dpy, *children_return[*nchildren_return - 2]);
}
else
{
// handle error
}
}
}
相关文章:
- 在某些循环内使用vector.push_back时出现分段错误
- 为什么在运行时没有向我们提供有关分段错误的更多信息?
- 如何解决gcc编译器优化导致的centos双编译器设置中的分段错误
- 当我的阵列太大时出现分段错误
- 分段错误当我试图运行程序时出错
- 在c++中初始化矩阵时出现分段错误(核心转储)
- 尝试使用集合函数时出现分段错误
- 我无法缩小此分段错误的原因
- g++的分段错误(在NaN上使用to_string两次时)
- 为什么我使用 std::copy() 收到运行时错误(分段错误)?
- 为什么我的代码包含错误分段错误(核心转储)?
- 为什么会出现此错误?- 分段错误(核心转储)
- 意外的运行时错误(分段错误)
- C++将文本文件读入数组,运行错误:分段错误
- mmap 错误:分段错误/指针无效错误
- 在我的代码中找不到错误....分段错误
- 错误:分段故障(核心转储)阵列
- 用string.h将错误分段到队列中
- c++字符串流转换错误:分段错误
- 为什么我的代码显示错误分段错误(核心转储)