Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: WxWidgets/SFML program crashes with an X window system error (BadAccess)  (Read 5662 times)

0 Members and 1 Guest are viewing this topic.

Sixmorphugus

  • Newbie
  • *
  • Posts: 3
    • View Profile
I have adapted this class from the given tutorials for SFML 1.6 and tried my best to update it for new APIs on both sides.

Code: [Select]
// .h FILE
#pragma once

#include <wx/wxprec.h>
#ifndef WX_PRECOMP
#include <wx/wx.h>
#endif

#include "SFML/Graphics.hpp"

class wxSFMLCanvas : public wxControl, public sf::RenderWindow
{
public:

wxSFMLCanvas(wxWindow* Parent = NULL, wxWindowID Id = -1, const wxPoint& Position = wxDefaultPosition,
const wxSize& Size = wxDefaultSize, long Style = 0);

virtual ~wxSFMLCanvas() {};

void setDrawMode(bool onOrOff);

private:

DECLARE_EVENT_TABLE()

virtual void OnUpdate() {};

void OnIdle(wxIdleEvent&);

void OnEraseBackground(wxEraseEvent&);

wxPaintDC* drawLock;
};

// .cpp FILE
#include "wxSFMLCanvas.h"

// some extras needed for gtk
#ifdef __WXGTK__
#include <gtk/gtk.h>
#include <gdk/gdkx.h>
#endif

BEGIN_EVENT_TABLE(wxSFMLCanvas, wxControl)
EVT_IDLE(wxSFMLCanvas::OnIdle)
EVT_ERASE_BACKGROUND(wxSFMLCanvas::OnEraseBackground)
END_EVENT_TABLE()

wxSFMLCanvas::wxSFMLCanvas(wxWindow* Parent, wxWindowID Id, const wxPoint& Position, const wxSize& Size, long Style) :
wxControl(Parent, Id, Position, Size, Style)
{
#ifdef __WXGTK__
GtkWidget* widget = GetHandle();

gtk_widget_realize(widget);
gtk_widget_set_double_buffered(widget, false);

GdkWindow* Win = gtk_widget_get_window( widget );

XFlush(GDK_WINDOW_XDISPLAY(Win));

sf::RenderWindow::create((sf::WindowHandle)GDK_WINDOW_XID(Win));
#else
// Tested under Windows XP only (should work with X11 and other Windows versions - no idea about MacOS)
sf::RenderWindow::create((sf::WindowHandle)GetHandle());
#endif

drawLock = nullptr;
}

void wxSFMLCanvas::OnIdle(wxIdleEvent&)
{
// Send a paint message when the control is idle, to ensure maximum framerate
Refresh();
}

void wxSFMLCanvas::OnEraseBackground(wxEraseEvent&)
{
}

void wxSFMLCanvas::setDrawMode(bool onOrOff)
{
if (onOrOff && !drawLock) {
drawLock = new wxPaintDC(this);
}
else if (!onOrOff && drawLock) {
delete drawLock;
drawLock = nullptr;
}
}

However, I'm having no luck on Linux. On Windows this works fine but on Linux I get the following in stderr:
Quote
The program 'MAGEditor' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
  (Details: serial 54 error_code 10 request_code 2 minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)
AL lib: ReleaseALC: 1 device not closed

If you wish to try the class for yourself it is trivial from this point to write a wxApp that uses a frame and reproduce the crash. Normally I would do this for you guys but I'm not too well so I'm going back to bed now /;

Any help would be appreciated.

victorlevasseur

  • Full Member
  • ***
  • Posts: 206
    • View Profile
Re: WxWidgets/SFML program crashes with an X window system error (BadAccess)
« Reply #1 on: January 24, 2017, 09:16:56 am »
Hi,

I also have the same problem (with SFML 2.4.1) and I'm really concerned as my app rely very much on the link between SFML and wxWidgets.  :(

You can test the bug on Linux with my app (doesn't happen on my computer but it seems to happen on the lastest Ubuntu). The app is GDevelop.

dmartins

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: WxWidgets/SFML program crashes with an X window system error (BadAccess)
« Reply #2 on: February 04, 2017, 05:46:23 am »
I'm having similar trouble. I've tried pretty much every variation of code I can find online to get the handle for a GtkWidget.
Arch Linux - SFML 2.4.1, wxwidgets 3.0 (gtk2).

I'm compiling gtk2 with debug symbols at the moment in the hopes of getting a meaningful backtrace.
Code: [Select]
#include <wx/wx.h>
#include <SFML/Graphics.hpp>
#include <gtk/gtk.h>
#include <gdk/gdkx.h>


class MyApp : public wxApp
{
    public:
        virtual bool OnInit();
};

class MyFrame : public wxFrame
{
    public:
        MyFrame();
};

class MyControl : public wxControl, public sf::RenderWindow
{
    public:
        MyControl(wxWindow *parent,
                  wxWindowID id,
                  const wxPoint &pos=wxDefaultPosition,
                  const wxSize &size=wxDefaultSize,
                  long style=0,
                  const wxValidator &validator=wxDefaultValidator,
                  const wxString &name=wxControlNameStr
                  );
       
        virtual ~MyControl(){};
};

bool MyApp::OnInit()
{
    wxFrame *frame = new MyFrame;
    frame->Show(true);
    return true;
}

MyFrame::MyFrame() : wxFrame(NULL,
                             wxID_ANY,
                             "Test",
                             wxDefaultPosition,
                             wxSize(800, 600))
{
    new MyControl(this, wxID_ANY);
}

MyControl::MyControl(wxWindow *parent,
                     wxWindowID id,
                     const wxPoint &pos,
                     const wxSize &size,
                     long style,
                     const wxValidator &validator,
                     const wxString &name) :
                     
                     wxControl(parent,
                               id,
                               pos,
                               size,
                               style,
                               validator,
                               name)
{
    GtkWidget *widget = m_wxwindow;
    if (!GTK_WIDGET_REALIZED(widget))
    {
        gtk_widget_realize(widget);
    }
    GdkWindow *gdkWin = widget->window;
    sf::RenderWindow(GDK_WINDOW_XWINDOW(gdkWin));
}

wxIMPLEMENT_APP(MyApp);


Code: [Select]
[dmartins@sirschmoopy tmp]$ g++ -g `wx-config --cxxflags` `pkg-config --cflags sfml-graphics` `pkg-config --cflags gtk+-2.0` -o test main.cpp `wx-config --libs` `pkg-config --libs sfml-graphics` `pkg-config --libs gtk+-2.0`

[dmartins@sirschmoopy tmp]$ ./test
The program 'test' received an X Window System error.
This probably reflects a bug in the program.
The error was 'BadAccess (attempt to access private resource denied)'.
  (Details: serial 45 error_code 10 request_code 2 minor_code 0)
  (Note to programmers: normally, X errors are reported asynchronously;
   that is, you will receive the error a while after causing it.
   To debug your program, run it with the --sync command line
   option to change this behavior. You can then get a meaningful
   backtrace from your debugger if you break on the gdk_x_error() function.)


dmartins

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: WxWidgets/SFML program crashes with an X window system error (BadAccess)
« Reply #3 on: February 04, 2017, 06:25:41 am »
So, I compiled gtk2, libx11, and sfml with debug symbols, and here is what I get for a back trace when breaking on gdk_x_error.
Versions used are:
libx11-1.6.4
gtk2-2.24.31
sfml-2.4.1
wxgtk-3.0.2

Code: [Select]
[dmartins@sirschmoopy tmp]$ g++ -g `wx-config --cxxflags` `pkg-config --cflags sfml-graphics` `pkg-config --cflags gtk+-2.0` -o test main.cpp `wx-config --libs` `pkg-config --libs sfml-graphics` `pkg-config --libs gtk+-2.0`
[dmartins@sirschmoopy tmp]$ gdb ./test
GNU gdb (GDB) 7.12.1
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-pc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
<http://www.gnu.org/software/gdb/documentation/>.
For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...done.
(gdb) break main
Breakpoint 1 at 0x409b1b: file main.cpp, line 78.
(gdb) run --sync
Starting program: /home/dmartins/tmp/test --sync
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/usr/lib/libthread_db.so.1".

Breakpoint 1, main (argc=2, argv=0x7fffffffe8a8) at main.cpp:78
78 wxIMPLEMENT_APP(MyApp);
(gdb) break gdk_x_error
Breakpoint 2 at 0x7ffff4ec7770: file gdkmain-x11.c, line 458.
(gdb) continue
Continuing.
[New Thread 0x7fffdbfba700 (LWP 6734)]
[New Thread 0x7fffdb5b4700 (LWP 6735)]
[New Thread 0x7fffdadb3700 (LWP 6736)]
[New Thread 0x7fffda5b2700 (LWP 6737)]
[New Thread 0x7fffd9db1700 (LWP 6738)]

Thread 1 "test" hit Breakpoint 2, gdk_x_error (display=0x75d000, error=0x7fffffffdec0) at gdkmain-x11.c:458
458 {
(gdb) bt
#0  0x00007ffff4ec7770 in gdk_x_error (display=0x75d000, error=0x7fffffffdec0) at gdkmain-x11.c:458
#1  0x00007fffeff476fd in _XError (dpy=dpy@entry=0x75d000, rep=rep@entry=0x9c3db0) at XlibInt.c:1434
#2  0x00007fffeff44627 in handle_error (dpy=0x75d000, err=0x9c3db0, in_XReply=<optimized out>) at xcb_io.c:199
#3  0x00007fffeff446e5 in handle_response (dpy=0x75d000, response=0x9c3db0, in_XReply=<optimized out>)
    at xcb_io.c:311
#4  0x00007fffeff455f8 in _XReply (dpy=dpy@entry=0x75d000, rep=rep@entry=0x7fffffffe0a0, extra=extra@entry=0, discard=discard@entry=1) at xcb_io.c:621
#5  0x00007fffeff302ff in XInternAtom (dpy=dpy@entry=0x75d000, name=0x7fffffffe1c0 "WM_PROTOCOLS", onlyIfExists=onlyIfExists@entry=0) at IntAtom.c:181
#6  0x00007ffff5974a4e in sf::priv::getAtom(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool) (name="WM_PROTOCOLS", onlyIfExists=onlyIfExists@entry=false)
    at /home/dmartins/abs/sfml/src/SFML/src/SFML/Window/Unix/Display.cpp:99
#7  0x00007ffff597a5dd in sf::priv::WindowImplX11::setProtocols() (this=this@entry=0x983cd0)
    at /home/dmartins/abs/sfml/src/SFML/src/SFML/Window/Unix/WindowImplX11.cpp:1198
#8  0x00007ffff597af67 in sf::priv::WindowImplX11::WindowImplX11(unsigned long) (this=0x983cd0, handle=62914604)
    at /home/dmartins/abs/sfml/src/SFML/src/SFML/Window/Unix/WindowImplX11.cpp:420
#9  0x00007ffff5973311 in sf::priv::WindowImpl::create(unsigned long) (handle=handle@entry=62914604)
    at /home/dmartins/abs/sfml/src/SFML/src/SFML/Window/WindowImpl.cpp:78
#10 0x00007ffff5972f5e in sf::Window::create(unsigned long, sf::ContextSettings const&) (this=0x7fffffffe3f0, handle=62914604, settings=...) at /home/dmartins/abs/sfml/src/SFML/src/SFML/Window/Window.cpp:141
#11 0x00007ffff5bc1ba8 in sf::RenderWindow::RenderWindow(unsigned long, sf::ContextSettings const&) (this=0x7fffffffe3f0, handle=62914604, settings=...) at /home/dmartins/abs/sfml/src/SFML/src/SFML/Graphics/RenderWindow.cpp:53
#12 0x0000000000409abe in MyControl::MyControl(wxWindow*, int, wxPoint const&, wxSize const&, long, wxValidator const&, wxString const&) (this=0x75c000, parent=0x755370, id=-1, pos=..., size=..., style=0, validator=..., name=...)
    at main.cpp:75
#13 0x00000000004098c1 in MyFrame::MyFrame() (this=0x755370) at main.cpp:50
#14 0x000000000040976a in MyApp::OnInit() (this=0x69ffa0) at main.cpp:39
#15 0x000000000040a32b in wxAppConsoleBase::CallOnInit() (this=0x69ffa0) at /usr/include/wx-3.0/wx/app.h:93
#16 0x00007ffff5edac42 in wxEntry(int&, wchar_t**) () at /usr/lib/libwx_baseu-3.0.so.0
#17 0x0000000000409b2e in main(int, char**) (argc=2, argv=0x7fffffffe8a8) at main.cpp:78

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
Here's my experience with SFML + WxWidgets: http://en.sfml-dev.org/forums/index.php?topic=21373.0 It runs on Ubuntu 14.04 with gtk2, and should work on Windows, too. Do make sure to read my second post in there.  :D

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
I don't know why I was so blinded out, i got the exact same problem. My code works up until SFML 2.2, but from 2.3 it causes this error (tested for 2.3.2, but pretty sure it starts from 2.3). Might be worth checking the code difference between these two versions.

Here are some similar cases and relevant pages I gathered:
http://stackoverflow.com/questions/34384253/with-a-simple-code-the-sfml-window-crashes-all-the-time
https://bugzilla.mozilla.org/show_bug.cgi?id=1271100
https://github.com/SFML/SFML/issues/936
http://stackoverflow.com/questions/34844966/sfml-sfrenderwindowcreatewindow-crashes-program
https://github.com/atom/atom/issues/6496
https://www.sfml-dev.org/faq.php (has some entries on errors)

It will take a while going through all of this. I got the feeling it's a compatibility issue, not directly a linking issue between SFML and WxW. Also it'd be worth checking if other already compiled SFML 2.4.2 programs run fine.

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
Spent some time on studying the five SFML functions in the back trace:
Quote
sf::priv::getAtom(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, bool)
sf::priv::WindowImplX11::setProtocols()
sf::priv::WindowImplX11::WindowImplX11(unsigned long)
sf::priv::WindowImpl::create(unsigned long)
sf::Window::create(unsigned long, sf::ContextSettings const&)
Compared version 2.1, which works for certain, and version 2.3.2, which gives the error for certain. Both sf::priv::WindowImpl::create(unsigned long) and sf::Window::create(unsigned long, sf::ContextSettings const&) are the same in both versions - there are no changes there.
There's some differences in sf::priv::WindowImplX11::WindowImplX11(unsigned long), although only one seems relevant - the newer one makes a call to sf::priv::WindowImplX11::setProtocols(), which exists only in 2.3.2, but not in 2.1.
The first thing setProtocols does, on its side, is xcb_atom_t wmProtocols = getAtom("WM_PROTOCOLS");, and according to the backtrace, the non-SFML function, in which the error occurs is exactly there. However, I couldn't find getAtom in the 2.3.2 source. I wonder what it does. Is it essential?


It'd be a good idea to read on the system functions further above in the backtrace:
Quote
#0  0x00007ffff4ec7770 in gdk_x_error (display=0x75d000, error=0x7fffffffdec0) at gdkmain-x11.c:458
#1  0x00007fffeff476fd in _XError (dpy=dpy@entry=0x75d000, rep=rep@entry=0x9c3db0) at XlibInt.c:1434
#2  0x00007fffeff44627 in handle_error (dpy=0x75d000, err=0x9c3db0, in_XReply=<optimized out>) at xcb_io.c:199
#3  0x00007fffeff446e5 in handle_response (dpy=0x75d000, response=0x9c3db0, in_XReply=<optimized out>)
    at xcb_io.c:311
#4  0x00007fffeff455f8 in _XReply (dpy=dpy@entry=0x75d000, rep=rep@entry=0x7fffffffe0a0, extra=extra@entry=0, discard=discard@entry=1) at xcb_io.c:621
#5  0x00007fffeff302ff in XInternAtom (dpy=dpy@entry=0x75d000, name=0x7fffffffe1c0 "WM_PROTOCOLS", onlyIfExists=onlyIfExists@entry=0) at IntAtom.c:181
gdk_x_error, _XError and handle_error seem to be only for error handling and reporting. This leaves us with only handle_response, _XReply and XInternAtom to investigate.
I don't really know how to interpret the data in the brackets, but looking at name=0x7fffffffe1c0 "WM_PROTOCOLS", onlyIfExists=onlyIfExists@entry=0, it seems like WM_PROTOCOLS is some entry that is missing. This further feeds my gut feelings that it's about gtk2 incompability and need to upgrade to gtk3. The latter, however, had proven troublesome in the past.

Searches like "WM_PROTOCOLS X window system error" don't turn up anything immediately seeming relevant.

Here's what Linux man page has to say about XInternAtom:
Quote
Atom XInternAtom(Display *display, char *atom_name, Bool only_if_exists);

atom_name
Specifies the name associated with the atom you want returned.

display
Specifies the connection to the X server.

only_if_exists
Specifies a Boolean value that indicates whether the atom must be created.

The XInternAtom function returns the atom identifier associated with the specified atom_name string. If only_if_exists is False, the atom is created if it does not exist. Therefore, XInternAtom can return None. If the atom name is not in the Host Portable Character Encoding, the result is implementation-dependent. Uppercase and lowercase matter; the strings ''thing'', ''Thing'', and ''thinG'' all designate different atoms. The atom will remain defined even after the client's connection closes. It will become undefined only when the last connection to the X server closes.

XInternAtom can generate BadAlloc and BadValue errors.
From what I can understand, a BadValue error is returned only when only_if_exists is true and the atom indentifier with such atom_name doesn't exist. But only_if_exists in this case is false, so this leaves BadAlloc as the only option. This is the trace to begin with next time. That, and reading the links I've posted above.

codecodecode

  • Newbie
  • *
  • Posts: 26
    • View Profile
I get this output on the console when opening the SFML window under version 2.1., maybe has something to do with the other issue:
Quote
libGL error: dlopen /usr/lib/i386-linux-gnu/dri/i915_dri.so failed (/usr/lib/i386-linux-gnu/dri/i915_dri.so: undefined symbol: _glapi_tls_Dispatch)
libGL error: unable to load driver: i915_dri.so
libGL error: driver pointer missing
libGL error: failed to load driver: i915
libGL error: dlopen /usr/lib/i386-linux-gnu/dri/swrast_dri.so failed (/usr/lib/i386-linux-gnu/dri/swrast_dri.so: undefined symbol: _glapi_tls_Dispatch)
libGL error: unable to load driver: swrast_dri.so
libGL error: failed to load driver: swrast
However, it doesn't crash or anything. Infact, it behaves normally just as expected and I was unaware of this until I made the console appear for when dealing with the crash issue.
A quick search on the internet shows that this is due to custom build opengl drivers or mesa, but I didn't custom build them. The search continues.

dmartins

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
codecodecode, thank you for your work on this! I'll go over the information you've provided in detail and report back anything new I can find.