[英]XOpenDisplay return display, but set errno to 11
I have a function,that open X11 display, do some stuff, and closes it.我有一个 function,它打开 X11 显示,做一些事情,然后关闭它。 I use errno to catch errors throughout the code.我使用 errno 来捕获整个代码中的错误。 After sometime debuffing I learned, that even though XOpenDisplay return the actual pointer (not NULL), errno is set to 11 in this function.经过一段时间的 debuff 我了解到,即使 XOpenDisplay 返回实际指针(不是 NULL),errno 在这个 function 中设置为 11。
int
take_screenshot()
{
/* Get display */
Display *display = XOpenDisplay(NULL);
/* XOpenDisplay may set errno to 11
* even though display is not NULL
*/
if (!display)
{
fprintf(stderr, "Error opening display: %s", strerror(errno));
return 1;
}
XCloseDisplay(display);
return 0;
}
It works fine, if add just errno = 0
after if statement
, but this looks like a bad idea.如果在if statement
后仅添加errno = 0
,它工作正常,但这看起来是个坏主意。 Also in man page it said that errno initial state is platform-dependent, but i check and it's value is 0 before calling XOpenDisplay
.同样在手册页中,它说 errno initial state 是平台相关的,但我检查了一下,在调用XOpenDisplay
之前它的值为 0。 Thank you in advance!先感谢您!
As @Some_programmer_dude metioned, I have mistread errno
, which I used as an error indicator, so I should simply check docs and use errno
only after NULL
check正如@Some_programmer_dude 提到的,我误解了errno
,我将其用作错误指示器,所以我应该简单地检查文档并仅在NULL
检查后使用errno
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.