gpt4 book ai didi

c++ - XQueryTree 上的段错误

转载 作者:行者123 更新时间:2023-11-28 05:36:06 26 4
gpt4 key购买 nike

我正在尝试关闭上次使用的窗口(按堆叠顺序位于当前窗口正下方的窗口)。不幸的是 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
}
}
}

关于c++ - XQueryTree 上的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38275229/

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