Search code examples
c++x11xlib

X11 resource freeing (in particular Screen objects)


I am currently working on a project where I occasionally need to use X11 functions directly. Coming from a windows world where everything is extensively documented, I am having some trouble finding and groking the relatively sparse linux documentation.

For instance, if I have the code:

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

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

XCloseDisplay(disp);

I know that XCloseDisplay will clean up Window root since it destroys all windows and resource IDs.

What I don't know is whether or not I need to manually XFree Screen objects returned from X functions like XDefaultScreenOfDisplay as per the example.

Any information on this case or even a generalized rule for when to use Xfree would be extremely helpful.


Solution

  • Raw X, like the Windows SDK is a C interface.

    There are about 7 manuals for X-Windows like 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). These are have been around since 1992. Like Windows, they are full of examples. Alternatively there is the internet - just type the command, most of the pages are online.

    The manual pages will tell you whether a command needs freeing or not.

    The man page for XDefaultScreenOfDisplay does not mention freeing so there is no need for it.

    If you look at the man page for XGetVisualInfo, it will say that you can free the structure returned usings XFree.