Find the answer to your Linux question:
Results 1 to 9 of 9
Hey guys, So I have this gui I created in Qt that embeds some X11 windows inside of it (and does a whole bunch of other stuff); when I close ...
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1
    Just Joined!
    Join Date
    Jul 2012
    Posts
    7

    Possible incorrect use of Xlib functions?


    Hey guys,

    So I have this gui I created in Qt that embeds some X11 windows inside of it (and does a whole bunch of other stuff); when I close the application, I get a seg fault right after/ inside of the last destructor called.

    It's entirely possible I missed something in one of the other portions of the gui, but due to the scarce Xlib documentation and the fact that I've been looking for the cause of the fault for a while now, I'm wondering if I'm misusing the Xlib functions.

    All I'm doing is creating a few XWindows and displaying stuff in them using a third party library, like so (the following code is called a few times).

    On closing the program, the X Windows disappear as you would expect, then right before the seg fault message is printed on the screen, they all pop up for a second then go away..this is why I have a feeling it's the X Windows.

    Code:
       
    Display *display;
    Screen *scr;
    Window win;
    
    display = XOpenDisplay (NULL);
    scr = ScreenOfDisplay (display, DefaultScreen (display) );
    win = XCreateSimpleWindow (display, DefaultRootWindow (display), 0, 0, 320, 240, 0, BlackPixel (display, DefaultScreen (display)), BlackPixel (display,  DefaultScreen (display)));
    XMapWindow (display, win);
    XFlush (display);
    
    //displaying stuff in X windows
    
    XDestroyWindow (display, win);
    XCloseDisplay (display);
    In particular, I couldn't find any good examples on closing them correctly; I'm not sure if XDestroyWindow() should be called before XCloseDisplay().

  2. #2
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,539
    Why are you using raw xlib functions inside of a Qt application? Use Qt windowing functions. They properly wrap all the X11 cruft for you.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  3. #3
    Just Joined!
    Join Date
    Jul 2012
    Posts
    7
    I didn't think there was a way to do this for sure in Qt...I've looked at the QX11EmbedWidget and QX11EmbedContainer; actually, right now the XWindows I created in that above snippet are being embedded in a QX11EmbedContainer; I don't think the QX11EmbedWidget can be used the same way as a native XWindow, can it? I've attempted to get it to behave like one with no luck :/

    here's my now four day old, pseudocode-like memory of what I attempted to do:

    Code:
    QX11EmbedContainer  *container = new QX11EmbedContainer();
    QX11EmbedWidget  *widgy = new QX11EmbedWidget();
    
    container.embedClient(widgy->winId);
    
    //third party function which displays stuff on widgy;
    As I recall, this didn't work very well at all..

    thanks, Rubberman!

  4. #4
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,539
    Which version of Qt are you using? Also, why use the X11 wrappers and not Qt classes directly? If you were to explain precisely what you are trying to accomplish, and why you can't use Qt to do that, it might help us point you in the right direction.

    Also, I am a senior Nokia engineer, so I have a lot of access to people who can help with your problem, provided I understand it well enough.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  5. #5
    Just Joined!
    Join Date
    Jul 2012
    Posts
    7
    I'm not on my dev machine, but I'm pretty sure i'm using Qt 4.7.

    Ok, so I have a third party API that gets image frames from a framegrabber card, and I would like to display them at a very fast framerate in the GUI. It runs fine and according to specifications, but on exit, the whole program segfaults.

    Right now, I am passing the window win, created in the first post, into one of those functions, which embeds another type of 'display' (not a X Display, its one that they define) into said window. From there, win is passed to an instance of another function (using signals), which embeds the window into a QX11EmbedWidget, using embedClient(). Then, I am calling their function that grabs a frame, does some processing, then displays the frame on that display, which is embedded into win. The displaying function is called several hundred times within a loop.

    To elaborate on my earlier post, I attempted to use a QX11EmbedWidget in place of the Xlib code in the first post, but to no avail (I don't remember exactly what went wrong though; I think it was another segmentation fault :/)

    Since posting this question, I've been able to narrow my problem down to the portion of code where I create that window (the code in the first post), or where the display buffer is painted into win.

    I'm pretty sure I'm not modifying any GUI components of my project outside of the main thread too.

    Thank you for your patience, Rubberman; I can see now that my previous post was really skimpy on the details.

  6. #6
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,539
    Thanks, this helps a lot. Let me consider what you are doing and I'll get back to you later. In any case, an observation - usually this sort of core dump (segfault) on exit is due to resources being multiply destructed. IE, you have some object that is passed to a container, which when destroyed, also destroys the objects you passed to it. Then, the object goes out of scope and its destructor is called again, but because it has already been deleted, a segfault occurs. So, in these sort of cases, I usually running the application in the debugger and then trap it when the segfault occurs, which will tell me what objects are causing the problem. Then, the solution usually is pretty simple, such as not using static/global object variables, or not placing a heap-based object in a smart pointer. Stuff like that.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  7. #7
    Just Joined!
    Join Date
    Jul 2012
    Posts
    7
    ehh, I found where the segmentation fault was coming from; it had something to do with XInitThreads()

    When I commented that out, program worked fine and didn't segfault, even though I'm using multiple threads...

    I'm still interested in a method of doing this with Qt instead of Xlib though.

  8. #8
    Linux Guru Rubberman's Avatar
    Join Date
    Apr 2009
    Location
    I can be found either 40 miles west of Chicago, in Chicago, or in a galaxy far, far away.
    Posts
    11,539
    Qt, on Linux, uses the Xlib functions under the covers, so it is probably already calling XInitThreads(). My "guess" is that this double initialization is what caused the problem. Not certain about that, but it seems to correlate to your system's behavior pretty well. In any case, I'm glad you got it sorted out.
    Sometimes, real fast is almost as good as real time.
    Just remember, Semper Gumbi - always be flexible!

  9. #9
    Just Joined!
    Join Date
    Jul 2012
    Posts
    7
    Ahh, that makes perfect sense. It also makes sense that Qt uses Xlib under the hood.

    Thanks alot Rubberman!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •