X11 资源释放(特别是 Screen 对象)

X11 resource freeing (in particular Screen objects)

我目前正在做一个偶尔需要直接使用X11函数的项目。来自一个 windows 世界,那里的一切都被广泛记录,我在寻找和摸索相对稀疏的 linux 文档时遇到了一些麻烦。

例如,如果我有代码:

Display* disp;
if (!(disp = XOpenDisplay(NULL))) {
    return;
}

Window root = DefaultRootWindow(disp);
Screen* scr = XDefaultScreenOfDisplay(disp);

XCloseDisplay(disp);

我知道 XCloseDisplay 会清理 Window root 因为它会破坏所有 windows 和资源 ID。

我不知道我是否需要根据示例手动 XFree Screen 从 X 函数返回的对象,例如 XDefaultScreenOfDisplay

关于这种情况的任何信息,甚至关于何时使用 Xfree 的一般规则都将非常有帮助。

Raw X,和WindowsSDK一样是C接口。

X-Windows 大约有 7 个手册,例如 http://shop.oreilly.com/product/9781565920026.do (Xlib Programming Manual) and https://www.oreilly.com/library/view/x-toolkit-intrinsics/9780937175620/(X Toolkit Intrinsics Programming Manual)。这些自 1992 年以来一直存在。像 Windows 一样,它们充满了示例。或者有互联网 - 只需键入命令,大部分页面都在线。

手册页会告诉您命令是否需要释放。

XDefaultScreenOfDisplay 的手册页没有提到释放,因此没有必要。

如果您查看 XGetVisualInfo 的手册页,它会说您可以释放使用 XFree 返回的结构。

Applications should not directly modify any part of the Display and Screen structures. The members should be considered read-only, although they may change as the result of other operations on the display.

这意味着这些结构由 XLib 管理,不应该 freeXFree 他们。释放一个结构意味着它曾经占用的内存可以分配给其他数据并最终被覆盖。