Simple PyQt5 QML application causes segmentation fault
<p>In trying to get a <a href="https://www.boxcontrol.net/beginners-pyqt5-and-qml-integration-tutorial.html" rel="nofollow">very basic PyQt5 QML example</a> to run, I found that I get a segmentation fault.  I verified that it only seems deal with displaying QML since <a href="http://pyqt.sourceforge.net/Docs/PyQt5/qml.html" rel="nofollow">an example without a window</a> runs fine.  I tried the following minimial test:</p>

<pre><code>#!/usr/bin/python3
import sys
from PyQt5.QtCore import QUrl
from PyQt5.QtWidgets import QGuiApplication
from PyQt5.QtQml import QQmlApplicationEngine

# Main Function
if __name__ == '__main__':
    app = QGuiApplication(sys.argv)
    engine = QQmlApplicationEngine("simple.qml")
    engine.quit.connect(app.quit)
    sys.exit(app.exec_())
</code></pre>

<p>simple.qml:</p>

<pre><code>import QtQuick 2.5
import QtQuick.Controls 1.4

ApplicationWindow {
    width: 300
    height: 200
    title: "Simple"
    visible: true
}
</code></pre>

<p>When I run this application, a window appears for a split second before closing like in the more detailed example, and I receive <code>Segmentation fault</code> in the console (and nothing more).</p>

<p>Running from GDB shows that the <code>QSGRenderThread</code> is receiving the SIGSEGV:</p>

<pre><code>(gdb) run snowman_qt.py 
Starting program: /usr/bin/python3 snowman_qt.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
[New Thread 0x7fffe912b700 (LWP 17200)]
[New Thread 0x7fffe3dbb700 (LWP 17201)]
[New Thread 0x7fffe1442700 (LWP 17202)]
[New Thread 0x7fffdbfff700 (LWP 17203)]

Thread 5 "QSGRenderThread" received signal SIGSEGV, Segmentation fault.
[Switching to Thread 0x7fffdbfff700 (LWP 17203)]
__strstr_sse2 (haystack_start=0x0, needle_start=0x7fffe28c9dd0 "nouveau") at ../string/strstr.c:63
63  ../string/strstr.c: No such file or directory.
</code></pre>

<p>The backtrace follows:</p>

<pre><code>#0  __strstr_sse2 (haystack_start=0x0, needle_start=0x7fffe28c9dd0 "nouveau") at ../string/strstr.c:63
#1  0x00007fffe27233ea in QSGRenderContext::initialize(QOpenGLContext*) ()
   from /usr/local/lib/python3.5/dist-packages/PyQt5/Qt/qml/QtQuick.2/../../lib/libQt5Quick.so.5
#2  0x00007fffe273e979 in ?? ()
   from /usr/local/lib/python3.5/dist-packages/PyQt5/Qt/qml/QtQuick.2/../../lib/libQt5Quick.so.5
#3  0x00007ffff56835f9 in ?? () from /usr/local/lib/python3.5/dist-packages/PyQt5/Qt/lib/libQt5Core.so.5
#4  0x00007ffff7bc16fa in start_thread (arg=0x7fffdbfff700) at pthread_create.c:333
#5  0x00007ffff78f7b5d in clone () at ../sysdeps/unix/sysv/linux/x86_64/clone.S:109
</code></pre>

<p>If I run the QML file <a href="https://github.com/thp/pyotherside/issues/63" rel="nofollow">from a C++ application</a>, there is no segmentation fault and the application works.  Note that I'm using PyQT without PyOtherside, but the symptoms seem similar.</p>

<p>Is there a way to get more information to continue debugging?</p>

<p>I am running Python 3.5.2 on Linux Mint 18.  My QT version is 5.7.0, my PyQt version is 5.7, and my SIP version is 4.18.1.</p>

<p>The rabbit hole went deeper than I thought.  There were a few problems.</p>

<ol>
<li>The version of SIP in the Linux Mint 18 (likely Ubuntu 16.04) repository was too old.  I updated to 4.18.1 by <a href="http://pyqt.sourceforge.net/Docs/sip4/installation.html" rel="nofollow">installing from source</a>.</li>
<li>With an updated SIP version, I was able to <a href="http://pyqt.sourceforge.net/Docs/PyQt5/installation.html" rel="nofollow">compile the latest PyQt5 from source</a>.  Before compiling, however, I had to install libqt5opengl5-dev so that it would compile with the QtOpenGL module.  After compiling, I installed using <code>checkinstall</code> to make rolling back easier.</li>
</ol>

<p>With the latest SIP and PyQt5, I no longer received the segmentation fault.  However, I had issues with the OpenGL shaders not being created properly and just received a plain white window.</p>

<p>The workaround for <a href="https://bugs.launchpad.net/ubuntu/+source/python-qt4/+bug/941826" rel="nofollow">this bug</a> is to include the following before loading PyQt in the Python program:</p>

<pre><code>import ctypes
from ctypes import util
ctypes.CDLL(util.find_library('GL'), ctypes.RTLD_GLOBAL)
</code></pre>

<p>I believe this is unique to Ubuntu-based OSs with NVIDIA graphics cards and binary drivers.</p>

python qt qml pyqt5