如何解读gdb输出?Python3.5+PyQt4程序段错误调试求助
Hey there! I’ve helped debug plenty of PyQt segmentation faults before, so let’s walk through how to make sense of that gdb output and track down your crash with Python 3.5 and PyQt4.
First: Capture the Right GDB Output
Before we interpret anything, let’s make sure you’re getting the most useful data from gdb. Follow these steps:
- Launch gdb with your Python interpreter:
gdb python - Run your script inside gdb:
run your_program.py - When the segmentation fault hits, run these commands to get critical info:
bt # Gets the full C/C++ backtrace py-bt # If your gdb has Python support, this gives a Python-level call stack (way easier to parse!) info locals # Shows local variables in the current crash frame
Next: Deciphering the Output
Let’s break down what you’ll see:
The bt (C Backtrace)
This shows the chain of C/C++ function calls leading to the crash. You’ll see three types of frames:
- Frames from
libpython3.5m.so: These are part of the Python interpreter itself. Unless you’re messing with CPython internals, these are usually just context. - Frames from
QtGui.so/QtCore.soorlibQt*.so: These are PyQt4’s C++ bindings or the Qt library itself. If the crash is here, it’s almost always related to Qt GUI rules being broken. - Frames from your Python code (they’ll reference
PyEval_EvalFrameExor similar, with hints about your script’s line numbers).
For example, if your backtrace looks like this:
#0 0x00007fxxxxxx in QWidget::show() () from /usr/lib/libQtGui.so.4 #1 0x00007fxxxxxx in sipQWidget::show() () from /usr/lib/python3.5/site-packages/PyQt4/QtGui.so #2 0x00007fxxxxxx in PyCFunction_Call () from /usr/lib/libpython3.5m.so.1.0 #3 0x00007fxxxxxx in PyObject_Call () from /usr/lib/libpython3.5m.so.1.0 #4 0x00007fxxxxxx in PyEval_EvalFrameEx () from /usr/lib/libpython3.5m.so.1.0 #5 0x00007fxxxxxx in PyEval_EvalCodeEx () from /usr/lib/libpython3.5m.so.1.0 #6 0x00007fxxxxxx in PyEval_EvalFrameEx () from /usr/lib/libpython3.5m.so.1.0 #7 0x00007fxxxxxx in PyEval_EvalCodeEx () from /usr/lib/libpython3.5m.so.1.0 #8 0x00007fxxxxxx in PyFunction_Call () from /usr/lib/libpython3.5m.so.1.0 #9 0x00007fxxxxxx in PyObject_Call () from /usr/lib/libpython3.5m.so.1.0 #10 0x00007fxxxxxx in QtCore::SignalInstance::emit(QtPrivate::QSlotObjectBase*, void**) () from /usr/lib/python3.5/site-packages/PyQt4/QtCore.so
The crash is happening when calling QWidget::show()—a huge red flag that you’re either:
- Trying to show a widget that’s already been destroyed, or
- Calling this GUI method from a non-main thread.
The py-bt (Python Backtrace)
If this works, it’s your best friend. It will show exactly which line of your Python code triggered the crash, like:
#0 <method 'show' of 'QWidget' objects> #1 main() at your_program.py:42
This directly points you to line 42 in your script—no guessing required.
Common PyQt4 + Python 3.5 Segmentation Fault Causes
Based on your setup, these are the most likely culprits:
- Non-main thread GUI operations: Qt strictly requires all UI interactions (creating widgets, updating labels, showing windows) to happen in the main thread. If you’re using
threadingormultiprocessingto update the UI, that’s almost certainly the issue. - Dangling widget references: If you delete a widget (either via
deleteLater()or Python’s garbage collection) but still have a variable pointing to it, calling methods on that stale reference will crash the program. - Version mismatches: Python 3.5 is pretty old, and some PyQt4 versions have compatibility bugs with it. Double-check you’re using the latest PyQt4 release that supports Python 3.5.
- C extension conflicts: If your program uses other C-based Python libraries (like numpy or OpenCV), there might be low-level conflicts with PyQt4’s Qt libraries.
Pro Tips to Narrow It Down
- Use
frame <frame-number>in gdb to jump to a specific stack frame, theninfo localsto see what variables were in use when it crashed. - Add debug prints to your Python code around UI operations—log when you create, modify, or destroy widgets. This can help you spot if a widget is being accessed after it’s supposed to be gone.
- Try to reproduce the crash consistently. If it’s random, it’s likely a race condition (like a thread accessing a widget at the same time as the main thread).
内容的提问来源于stack exchange,提问作者Kajsa

