所以我一直在玩XLib,我尝试用c ++编写一个程序,只需截取屏幕并在窗口中显示,从而产生一种隧道效果。它工作得很好,只需几秒钟。然后我的计算机(顺便运行Ubuntu)开始以非常非常慢的帧速率,我的鼠标几乎没有响应,关闭窗口无法停止它并按ctrl-c无法停止它,然后它只是冻结完全,我没有选择使用我的电源按钮手动关闭它并重新启动计算机(之后,它似乎工作正常?)。
现在,XLib是一个非常奇怪的库,没有很好的文档,所以我知道我从根本上做错了什么,但我的代码很简单,我不知道它可能是什么。我已经尝试了各种各样的东西(注释掉某些功能,使用usleep将它暂停在帧之间),但是当它不起作用并且必须重新启动计算机时,每个新想法都令人沮丧。所以也许你们其中一个人知道发生了什么事?
这是我的代码:
#include <X11/Xlib.h>
int main(){
//Initializes some values
Display* display=XOpenDisplay(0);
Window root=DefaultRootWindow(display);
XWindowAttributes att;
XGetWindowAttributes(display, root, &att);
//att.width and att.height now are the width and height of the screen
int screen=DefaultScreen(display);
Window window=XCreateSimpleWindow(display, root, 0, 0,
att.width, att.height, 1,
BlackPixel(display, screen),
WhitePixel(display, screen));
GC graphics=XCreateGC(display, window, 0, NULL);
//puts window on screen
XMapWindow(display, window);
while(1){
//This gets the screenshot
XImage* ximg=XGetImage(display, root, 0, 0,
att.width, att.height,
AllPlanes, ZPixmap);
//This puts the screenshot in the window "window"
XPutImage(display,window, graphics, ximg,
0,0,0,0, att.width, att.height);
//frees the data allocated to ximg
XFree(ximg);
}
}
(这应该用简单的g ++ screentest.cpp -lX11编译)
对于它的价值,我从未收集过任何解决方案的一些信息性网页是:
The man page for XGetImage and XPutImage
The man page for XFree, although I really don't think this is the problem
This Stack Overflow question seems relevant - but it doesn't have any answers
我还查看过相当多的其他Stack Overflow问题,虽然相当数量看起来可能相关,但我没有看到与我的问题直接相关的任何内容。
感谢您提供任何帮助。
答案 0 :(得分:0)
你说你在通话之间添加了代码来睡觉。那将是一件好事。尝试添加比您已尝试过的更长的睡眠间隔。
另外考虑使用计数器,这样你就不会永远调用它。我猜这个问题要么是逃避内存使用,要么是无限循环。无论哪种方式,放入一些东西,以便程序将自行停止,而不是依靠你手动退出它。